LYGIA Shader Library

rgb2hue (lygia/v1.1.6/color/space/rgb2hue)

pass a color in RGB and get HSB color. From http://lolengine.net/blog/2013/07/27/rgb-to-hsv-in-glsl

Use:

<float> rgb2hue(<vec3|vec4> color)

Check it on Github


#ifndef FNC_RGB2HUE
#define FNC_RGB2HUE
float rgb2hue(in vec3 c) {
    vec4 K = vec4(0., -.33333333333333333333, .6666666666666666666, -1.);

#ifdef RGB2HSV_MIX
    vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
    vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
#else
    vec4 p = c.g < c.b ? vec4(c.bg, K.wz) : vec4(c.gb, K.xy);
    vec4 q = c.r < p.x ? vec4(p.xyw, c.r) : vec4(c.r, p.yzx);
#endif

    float d = q.x - min(q.w, q.y);
    float e = 1.0e-10;
    return abs(q.z + (q.w - q.y) / (6. * d + e));
}

float rgb2hue(in vec4 c) {
    return rgb2hue(c.rgb);
}
#endif

Use:

<float> rgb2hue(<float3|float4> color)

Check it on Github


#ifndef FNC_RGB2HUE
#define FNC_RGB2HUE
float rgb2hue(in float3 c) {
    float4 K = float4(0., -.33333333333333333333, .6666666666666666666, -1.);

#ifdef RGB2HSV_MIX
    float4 p = lerp(float4(c.bg, K.wz), float4(c.gb, K.xy), step(c.b, c.g));
    float4 q = lerp(float4(p.xyw, c.r), float4(c.r, p.yzx), step(p.x, c.r));
#else
    float4 p = c.g < c.b ? float4(c.bg, K.wz) : float4(c.gb, K.xy);
    float4 q = c.r < p.x ? float4(p.xyw, c.r) : float4(c.r, p.yzx);
#endif

    float d = q.x - min(q.w, q.y);
    float e = 1.0e-10;
    return abs(q.z + (q.w - q.y) / (6. * d + e));
}

float rgb2hue(in float4 c) {
    return rgb2hue(c.rgb);
}
#endif

Dependencies:

Check it on Github


fn rgb2hue(color: vec3<f32>) -> f32 {
    return rgb2hsv(color).x;
}

LYGIA is dual-licensed under the Prosperity License and the Patron License for sponsors and contributors.

Sponsors and contributors are automatically added to the Patron License and they can ignore the any non-commercial rule of the Prosperity Licensed software (please take a look to the exception).

It's also possible to get a permanent comercial license hook to a single and specific version of LYGIA.

Get the latest news and releases

Sign up for the news letter bellow, joing the LYGIA's channel on Discord or follow the Github repository