fork(2) download
  1. #include <iostream>
  2. #include <random>
  3.  
  4. struct Color {
  5. Color(unsigned char r, unsigned char g, unsigned char b) : r(r), g(g), b(b) {}
  6. unsigned char r, g, b;
  7. };
  8.  
  9. std::ostream &operator<<(std::ostream &out, const Color &c) {
  10. return out << "(" << (int)c.r << ", " << (int)c.g << ", " << (int)c.b << ")";
  11. }
  12.  
  13. Color random_color(int j, std::mt19937 &gen) {
  14. std::uniform_int_distribution<int> distrib(1, 0xFFFFFF);
  15. int color = distrib(gen);
  16. int r = color & 0xFF;
  17. int g = (color >> 8) & 0xFF;
  18. int b = (color >> 16) & 0xFF;
  19.  
  20. double l = sqrt(0.299*r*r + 0.587*g*g + 0.114*b*b);
  21. double c = j/l;
  22. return Color(c*r, c*g, c*b);
  23. }
  24.  
  25.  
  26. int main() {
  27. std::random_device rd;
  28. std::mt19937 gen(rd());
  29.  
  30. for (int j : { 0, 1, 2, 10, 50, 100, 200, 243, 254, 255 }) {
  31. std::cout << "j = " << j << ", color = " << random_color(j, gen) << std::endl;
  32. }
  33. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
j = 0, color = (0, 0, 0)
j = 1, color = (0, 1, 1)
j = 2, color = (1, 1, 4)
j = 10, color = (8, 10, 12)
j = 50, color = (54, 25, 104)
j = 100, color = (54, 116, 99)
j = 200, color = (249, 189, 55)
j = 243, color = (165, 26, 190)
j = 254, color = (85, 65, 119)
j = 255, color = (49, 245, 130)