Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- cocos2d::Color4F HelloWorld::RGB2HSL(cocos2d::Color4F c)
- {
- float h, s, l;
- Color4F color;
- color.r = c.r;
- color.g = c.g;
- color.b = c.b;
- float cmax = fmax(fmax(color.r, color.g), color.b);
- float cmin = fmin(fmin(color.r, color.g), color.b);
- float delta = cmax - cmin;
- // Hue
- if (delta == 0)
- h = 0;
- else if (cmax == color.r)
- h = 60 * fmod((color.g - color.b) / delta, 6);
- else if (cmax == color.g)
- h = 60 * (((color.b - color.r) / delta) + 2);
- else if (cmax == color.b)
- h = 60 * (((color.r - color.g) / delta) + 4);
- // Lightness
- l = (cmax + cmin) / 2;
- // Saturation
- if (delta == 0)
- s = 0;
- else
- s = 1 - fabs(2 * l - 1);
- return Color4F(h, s, l, 0);
- }
- cocos2d::Color4F HelloWorld::HSL2RGB(cocos2d::Color4F color)
- {
- float h, s, l;
- float c, x, m;
- float r, g, b;
- h = clampf(color.r, 0, 360);
- s = clampf(color.g, 0, 1);
- l = clampf(color.b, 0, 1);
- c = (1 - (fabs(2 * l) - 1)) * s;
- x = c * (1 - fabs(fmod(h / 60, 2) - 1));
- m = l - (c / 2);
- if (h >= 0 && h < 60)
- {
- r = c;
- g = x;
- b = 0;
- }
- else if (h >= 60 && h < 120)
- {
- r = x;
- g = c;
- b = 0;
- }
- else if (h >= 120 && h < 180)
- {
- r = 0;
- g = c;
- b = x;
- }
- else if (h >= 180 && h < 240)
- {
- r = 0;
- g = x;
- b = c;
- }
- else if (h >= 240 && h < 300)
- {
- r = x;
- g = 0;
- b = c;
- }
- else if (h >= 60 && h < 120)
- {
- r = c;
- g = 0;
- b = x;
- }
- r = (r + m);
- g = (g + m);
- b = (b + m);
- return Color4F(r, g, b, 1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement