Advertisement
Ludwiq

Untitled

Dec 18th, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. #define GREEN 9
  2. #define BLUE 5
  3. #define RED 6
  4. #define delayTime 20
  5.  
  6. void setup()
  7. {
  8.  
  9.     pinMode(GREEN, OUTPUT);
  10.     pinMode(BLUE, OUTPUT);
  11.     pinMode(RED, OUTPUT);
  12.     digitalWrite(GREEN, HIGH);
  13.     digitalWrite(BLUE, HIGH);
  14.     digitalWrite(RED, HIGH);
  15. }
  16.  
  17. class RGB
  18. {
  19. public:
  20.     unsigned char R;
  21.     unsigned char G;
  22.     unsigned char B;
  23.  
  24.     RGB(unsigned char r, unsigned char g, unsigned char b)
  25.     {
  26.         R = r;
  27.         G = g;
  28.         B = b;
  29.     }
  30.  
  31.     bool Equals(RGB rgb)
  32.     {
  33.         return (R == rgb.R) && (G == rgb.G) && (B == rgb.B);
  34.     }
  35. };
  36.  
  37. class HSL
  38. {
  39. public:
  40.     int H;
  41.     float S;
  42.     float L;
  43.  
  44.     HSL(int h, float s, float l)
  45.     {
  46.         H = h;
  47.         S = s;
  48.         L = l;
  49.     }
  50.  
  51.     bool Equals(HSL hsl)
  52.     {
  53.         return (H == hsl.H) && (S == hsl.S) && (L == hsl.L);
  54.     }
  55. };
  56.  
  57. static float HueToRGB(float v1, float v2, float vH) {
  58.     if (vH < 0)
  59.         vH += 1;
  60.  
  61.     if (vH > 1)
  62.         vH -= 1;
  63.  
  64.     if ((6 * vH) < 1)
  65.         return (v1 + (v2 - v1) * 6 * vH);
  66.  
  67.     if ((2 * vH) < 1)
  68.         return v2;
  69.  
  70.     if ((3 * vH) < 2)
  71.         return (v1 + (v2 - v1) * ((2.0f / 3) - vH) * 6);
  72.  
  73.     return v1;
  74. }
  75.  
  76. static RGB HSLToRGB(HSL hsl) {
  77.     unsigned char r = 0;
  78.     unsigned char g = 0;
  79.     unsigned char b = 0;
  80.  
  81.     if (hsl.S == 0)
  82.     {
  83.         r = g = b = (unsigned char)(hsl.L * 255);
  84.     }
  85.     else
  86.     {
  87.         float v1, v2;
  88.         float hue = (float)hsl.H / 360;
  89.  
  90.         v2 = (hsl.L < 0.5) ? (hsl.L * (1 + hsl.S)) : ((hsl.L + hsl.S) - (hsl.L * hsl.S));
  91.         v1 = 2 * hsl.L - v2;
  92.  
  93.         r = (unsigned char)(255 * HueToRGB(v1, v2, hue + (1.0f / 3)));
  94.         g = (unsigned char)(255 * HueToRGB(v1, v2, hue));
  95.         b = (unsigned char)(255 * HueToRGB(v1, v2, hue - (1.0f / 3)));
  96.     }
  97.  
  98.     return RGB(r, g, b);
  99. }
  100.  
  101. int redVal;
  102. int blueVal;
  103. int greenVal;
  104.  
  105. void loop()
  106. {
  107.     //int[] colors = hslToRgb(2, 2, 2);
  108.     HSL data = HSL(138, 0.50f, 0.76f);
  109.     RGB value = HSLToRGB(data);
  110.  
  111.     /*analogWrite(RED, rgb.r);
  112.     analogWrite(GREEN, rgb.g);
  113.     analogWrite(BLUE, rgb.b);*/
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement