LYGIA Shader Library

rgb2hsv (lygia/color/space/rgb2hsv)

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

Use:

rgb2hsv(<vec3|vec4> color)

Check it on Github

// #include "rgb2hcv.glsl"


#ifndef HCV_EPSILON
#define HCV_EPSILON 1e-10
#endif

#ifndef FNC_RGB2HSV
#define FNC_RGB2HSV
vec3 rgb2hsv(const in vec3 c) {
    // vec3 HCV = rgb2hcv(rgb);
    // float S = HCV.y / (HCV.z + HCV_EPSILON);
    // return vec3(HCV.x, S, HCV.z);
    vec4 K = vec4(0., -0.33333333333333333333, 0.6666666666666666666, -1.0);
    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);
    float d = q.x - min(q.w, q.y);
    return vec3(abs(q.z + (q.w - q.y) / (6. * d + HCV_EPSILON)), 
                d / (q.x + HCV_EPSILON), 
                q.x);
}
vec4 rgb2hsv(const in vec4 c) { return vec4(rgb2hsv(c.rgb), c.a); }
#endif

Use:

rgb2hsv(<float3|float4> color)

Check it on Github


#ifndef FNC_RGB2HSV
#define FNC_RGB2HSV
float3 rgb2hsv(in float3 c) {
    float4 K = float4(0.0, -0.33333333333333333333, 0.6666666666666666666, -1.);
    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);
    float d = q.x - min(q.w, q.y);
    float e = 1.0e-10;
    return float3(  abs(q.z + (q.w - q.y) / (6. * d + e)), 
                    d / (q.x + e), 
                    q.x );
}
float4 rgb2hsv(in float4 c) { return float4(rgb2hsv(c.rgb), c.a); }
#endif

Check it on Github

fn rgb2hsv(c: vec3f) -> vec3f {
    let K = vec4f(0.0, -0.33333333333333333333, 0.6666666666666666666, -1.0);
    let p = mix(vec4f(c.bg, K.wz), vec4f(c.gb, K.xy), step(c.b, c.g));
    let q = mix(vec4f(p.xyw, c.r), vec4f(c.r, p.yzx), step(p.x, c.r));
    let d = q.x - min(q.w, q.y);
    let e = 1.0e-10;
    return vec3f(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}

License

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