LYGIA Shader Library

rgb2hcv (lygia/v1.2.0/color/space/rgb2hcv)

Convert from RGB to HCV (Hue, Chroma, Value) Based on work by Sam Hocevar and Emil Persson

Use:

rgb2xyz(<vec3|vec4> color)

Check it on Github


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

#ifndef FNC_RGB2HCV
#define FNC_RGB2HCV
vec3 rgb2hcv(const in vec3 rgb) {
    vec4 P = (rgb.g < rgb.b) ? vec4(rgb.bg, -1.0, 2.0/3.0) : vec4(rgb.gb, 0.0, -1.0/3.0);
    vec4 Q = (rgb.r < P.x) ? vec4(P.xyw, rgb.r) : vec4(rgb.r, P.yzx);
    float C = Q.x - min(Q.w, Q.y);
    float H = abs((Q.w - Q.y) / (6.0 * C + HCV_EPSILON) + Q.z);
    return vec3(H, C, Q.x);
}
vec4 rgb2hcv(vec4 rgb) {return vec4(rgb2hcv(rgb.rgb), rgb.a);}
#endif

Use:

<float3|float4> rgb2xyz(<float3|float4> rgb)

Check it on Github


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

#ifndef FNC_RGB2HCV
#define FNC_RGB2HCV
float3 rgb2hcv(float3 rgb) {
    float4 P = (rgb.g < rgb.b) ? float4(rgb.bg, -1.0, 2.0/3.0) : float4(rgb.gb, 0.0, -1.0/3.0);
    float4 Q = (rgb.r < P.x) ? float4(P.xyw, rgb.r) : float4(rgb.r, P.yzx);
    float C = Q.x - min(Q.w, Q.y);
    float H = abs((Q.w - Q.y) / (6.0 * C + HCV_EPSILON) + Q.z);
    return float3(H, C, Q.x);
}
float4 rgb2hcv(float4 rgb) {return float4(rgb2hcv(rgb.rgb), rgb.a);}
#endif

Check it on Github

fn rgb2hcv(rgb: vec3f) -> vec3f {
    let P = (rgb.g < rgb.b) ? vec4f(rgb.bg, -1.0, 2.0/3.0) : vec4f(rgb.gb, 0.0, -1.0/3.0);
    let Q = (rgb.r < P.x) ? vec4f(P.xyw, rgb.r) : vec4f(rgb.r, P.yzx);
    let C = Q.x - min(Q.w, Q.y);
    let H = abs((Q.w - Q.y) / (6.0 * C + 1e-10) + Q.z);
    return vec3f(H, C, 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