Browse Source

color: a reasonably fast RGB to HSV conversion with only three tests.

legacy
Sam Hocevar sam 12 years ago
parent
commit
d9bdb33bc0
1 changed files with 27 additions and 0 deletions
  1. +27
    -0
      src/lol/image/color.h

+ 27
- 0
src/lol/image/color.h View File

@@ -79,6 +79,33 @@ public:
return vec4(HSVToRGB(src.rgb), src.a);
}

/*
* Convert RGB to HSV
*/
static vec3 RGBToHSV(vec3 src)
{
float offset = 0.f;

if (src.r < src.g)
src = src.grb, offset = -2.f / 6.f;

if (src.g < src.b)
src = src.rbg, offset = -1.f - 4.f * offset;

if (src.r < src.g)
src = src.grb, offset = -2.f / 6.f - offset;

float chroma = src.r - src.b;
return vec3(abs((src.g - src.b) / (6.f * chroma + 1e-40f) + offset),
chroma / (src.r + 1e-40f),
src.r);
}

static vec4 RGBToHSV(vec4 src)
{
return vec4(RGBToHSV(src.rgb), src.a);
}

/*
* Convert linear HSV to linear HSL
*/


Loading…
Cancel
Save