LYGIA Shader Library

ryb2rgb (lygia/color/space/ryb2rgb)

Convert from RYB to RGB color space. Based on http://nishitalab.org/user/UEI/publication/Sugita_IWAIT2015.pdf http://vis.computer.org/vis2004/DVD/infovis/papers/gossett.pdf

Dependencies:

Use:

<vec3|vec4> ryb2rgb(<vec3|vec4> ryb)

Check it on Github



#ifndef RYB_LERP 
#define RYB_LERP(A, B, t) cubicMix(A, B, t)
#endif

#ifndef FNC_RYB2RGB
#define FNC_RYB2RGB

#ifdef RYB_FAST

vec3 ryb2rgb(vec3 ryb) {
    // Remove the white from the color
    float w = mmin(ryb);
    ryb -= w;

    float max_y = mmax(ryb);

    // Get the green out of the yellow & blue
    float g = mmin(ryb.gb);
    vec3 rgb = ryb - vec3(0., g, g);

    if (rgb.b > 0. && g > 0.) {
        rgb.b *= 2.;
        g *= 2.;
    }

    // Redistribute the remaining yellow.
    rgb.r += rgb.g;
    rgb.g += g;

    // Normalize to values.
    float max_g = mmax(rgb);
    rgb *= (max_g > 0.) ? max_y / max_g : 1.;

    // Add the white back in.        
    return rgb + w;
}

#else

vec3 ryb2rgb(vec3 ryb) {
    const vec3 ryb000 = vec3(1., 1., 1.);       // white
    const vec3 ryb100 = vec3(1., 0., 0.);       // Red          
    const vec3 ryb010 = vec3(1., 1., 0.);       // Yellow
    const vec3 ryb110 = vec3(1., .5, 0.);       // Orange
    const vec3 ryb001 = vec3(.163, .373, .6);   // blue
    const vec3 ryb101 = vec3(.5, 0., .5);       // Purple
    const vec3 ryb011 = vec3(0., .66, .2);      // Green
    const vec3 ryb111 = vec3(0., 0., 0.);       // Black
    return RYB_LERP(RYB_LERP(
        RYB_LERP(ryb000, ryb001, ryb.z),
        RYB_LERP(ryb010, ryb011, ryb.z),
        ryb.y), RYB_LERP(
        RYB_LERP(ryb100, ryb101, ryb.z),
        RYB_LERP(ryb110, ryb111, ryb.z),
        ryb.y), ryb.x);
}

#endif

vec4 ryb2rgb(vec4 ryb) { return vec4(ryb2rgb(ryb.rgb), ryb.a); }

#endif

Dependencies:

Use:

<float3|float4> ryb2rgb(<float3|float4> ryb)

Check it on Github



#ifndef RYB_LERP 
#define RYB_LERP(A, B, t) cubicMix(A, B, t)
#endif

#ifndef FNC_RYB2RGB
#define FNC_RYB2RGB

#ifdef RYB_FAST

float3 ryb2rgb(float3 ryb) {
    // Remove the white from the color
    float w = mmin(ryb);
    ryb -= w;

    float max_y = mmax(ryb);

    // Get the green out of the yellow & blue
    float g = mmin(ryb.gb);
    float3 rgb = ryb - float3(0., g, g);

    if (rgb.b > 0. && g > 0.) {
        rgb.b *= 2.;
        g *= 2.;
    }

    // Redistribute the remaining yellow.
    rgb.r += rgb.g;
    rgb.g += g;

    // Normalize to values.
    float max_g = mmax(rgb);
    rgb *= (max_g > 0.) ? max_y / max_g : 1.;

    // Add the white back in.        
    return rgb + w;
}

#else

float3 ryb2rgb(float3 ryb) {
    const float3 ryb000 = float3(1., 1., 1.);       // white
    const float3 ryb001 = float3(.163, .373, .6);   // blue
    const float3 ryb010 = float3(1., 1., 0.);       // Yellow
    const float3 ryb100 = float3(1., 0., 0.);       // Red          
    const float3 ryb011 = float3(0., .66, .2);      // Green
    const float3 ryb101 = float3(.5, 0., .5);       // Violet
    const float3 ryb110 = float3(1., .5, 0.);       // Orange
    const float3 ryb111 = float3(0., 0., 0.);       // Black
    return RYB_LERP(RYB_LERP(
        RYB_LERP(ryb000, ryb001, ryb.z),
        RYB_LERP(ryb010, ryb011, ryb.z),
        ryb.y), RYB_LERP(
        RYB_LERP(ryb100, ryb101, ryb.z),
        RYB_LERP(ryb110, ryb111, ryb.z),
        ryb.y), ryb.x);
}

#endif

float4 ryb2rgb(float4 ryb) { return float4(ryb2rgb(ryb.rgb), ryb.a); }

#endif

Dependencies:

Use:

<float3|float4> ryb2rgb(<float3|float4> ryb)

Check it on Github



#ifndef RYB_LERP 
#define RYB_LERP(A, B, t) cubicMix(A, B, t)
#endif

#ifndef FNC_RYB2RGB
#define FNC_RYB2RGB

#ifdef RYB_FAST

float3 ryb2rgb(float3 ryb) {
    // Remove the white from the color
    float w = mmin(ryb);
    ryb -= w;

    float max_y = mmax(ryb);

    // Get the green out of the yellow & blue
    float g = mmin(ryb.gb);
    float3 rgb = ryb - float3(0., g, g);

    if (rgb.b > 0. && g > 0.) {
        rgb.b *= 2.;
        g *= 2.;
    }

    // Redistribute the remaining yellow.
    rgb.r += rgb.g;
    rgb.g += g;

    // Normalize to values.
    float max_g = mmax(rgb);
    rgb *= (max_g > 0.) ? max_y / max_g : 1.;

    // Add the white back in.        
    return rgb + w;
}

#else

float3 ryb2rgb(float3 ryb) {
    constant float3 ryb000 = float3(1., 1., 1.);       // white
    constant float3 ryb100 = float3(1., 0., 0.);       // Red          
    constant float3 ryb010 = float3(1., 1., 0.);       // Yellow
    constant float3 ryb110 = float3(1., .5, 0.);       // Orange
    constant float3 ryb001 = float3(.163, .373, .6);   // blue
    constant float3 ryb101 = float3(.5, 0., .5);       // Purple
    constant float3 ryb011 = float3(0., .66, .2);      // Green
    constant float3 ryb111 = float3(0., 0., 0.);       // Black
    return RYB_LERP(RYB_LERP(
        RYB_LERP(ryb000, ryb001, ryb.z),
        RYB_LERP(ryb010, ryb011, ryb.z),
        ryb.y), RYB_LERP(
        RYB_LERP(ryb100, ryb101, ryb.z),
        RYB_LERP(ryb110, ryb111, ryb.z),
        ryb.y), ryb.x);
}

#endif

float4 ryb2rgb(float4 ryb) { return float4(ryb2rgb(ryb.rgb), ryb.a); }

#endif

Dependencies:

Use:

<vec3> ryb2rgb(<vec3> ryb)

Check it on Github



fn ryb2rgb(ryb: vec3f) -> vec3f {
    let ryb000 = vec3f(1., 1., 1.);       // White
    let ryb001 = vec3f(.163, .373, .6);   // Blue
    let ryb010 = vec3f(1., 1., 0.);       // Yellow
    let ryb100 = vec3f(1., 0., 0.);       // Red          
    let ryb011 = vec3f(0., .66, .2);      // Green
    let ryb101 = vec3f(.5, 0., .5);       // Violet
    let ryb110 = vec3f(1., .5, 0.);       // Orange
    let ryb111 = vec3f(0., 0., 0.);       // Black
    return cubicMix(cubicMix(
        cubicMix(ryb000, ryb001, ryb.z),
        cubicMix(ryb010, ryb011, ryb.z),
        ryb.y), cubicMix(
        cubicMix(ryb100, ryb101, ryb.z),
        cubicMix(ryb110, ryb111, ryb.z),
        ryb.y), ryb.x);
}

Examples

Licenses

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