Advertisement
Guest User

Untitled

a guest
Jul 6th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. cocos2d::Color4F HelloWorld::RGB2HSL(cocos2d::Color4F c)
  2. {
  3. float h, s, l;
  4. Color4F color;
  5.  
  6. color.r = c.r;
  7. color.g = c.g;
  8. color.b = c.b;
  9.  
  10. float cmax = fmax(fmax(color.r, color.g), color.b);
  11. float cmin = fmin(fmin(color.r, color.g), color.b);
  12. float delta = cmax - cmin;
  13.  
  14. // Hue
  15. if (delta == 0)
  16. h = 0;
  17. else if (cmax == color.r)
  18. h = 60 * fmod((color.g - color.b) / delta, 6);
  19. else if (cmax == color.g)
  20. h = 60 * (((color.b - color.r) / delta) + 2);
  21. else if (cmax == color.b)
  22. h = 60 * (((color.r - color.g) / delta) + 4);
  23.  
  24. // Lightness
  25. l = (cmax + cmin) / 2;
  26.  
  27. // Saturation
  28. if (delta == 0)
  29. s = 0;
  30. else
  31. s = 1 - fabs(2 * l - 1);
  32.  
  33.  
  34. return Color4F(h, s, l, 0);
  35. }
  36.  
  37. cocos2d::Color4F HelloWorld::HSL2RGB(cocos2d::Color4F color)
  38. {
  39. float h, s, l;
  40. float c, x, m;
  41. float r, g, b;
  42.  
  43. h = clampf(color.r, 0, 360);
  44. s = clampf(color.g, 0, 1);
  45. l = clampf(color.b, 0, 1);
  46.  
  47. c = (1 - (fabs(2 * l) - 1)) * s;
  48. x = c * (1 - fabs(fmod(h / 60, 2) - 1));
  49. m = l - (c / 2);
  50.  
  51. if (h >= 0 && h < 60)
  52. {
  53. r = c;
  54. g = x;
  55. b = 0;
  56. }
  57. else if (h >= 60 && h < 120)
  58. {
  59. r = x;
  60. g = c;
  61. b = 0;
  62. }
  63. else if (h >= 120 && h < 180)
  64. {
  65. r = 0;
  66. g = c;
  67. b = x;
  68. }
  69. else if (h >= 180 && h < 240)
  70. {
  71. r = 0;
  72. g = x;
  73. b = c;
  74. }
  75. else if (h >= 240 && h < 300)
  76. {
  77. r = x;
  78. g = 0;
  79. b = c;
  80. }
  81. else if (h >= 60 && h < 120)
  82. {
  83. r = c;
  84. g = 0;
  85. b = x;
  86. }
  87.  
  88. r = (r + m);
  89. g = (g + m);
  90. b = (b + m);
  91.  
  92. return Color4F(r, g, b, 1);
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement