Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
| Comment: | pixel_robot: Started adding OKLAB/OKLCH color spaces. | 
|---|---|
| Downloads: | Tarball | ZIP archive | SQL archive | 
| Timelines: | family | ancestors | descendants | both | trunk | 
| Files: | files | file ages | folders | 
| SHA3-256: | 65eea726c6222ed2f808e1898e3ddc1d | 
| User & Date: | andy 2025-04-29 22:46:02 | 
Context
| 2025-04-30 | ||
| 16:03 | pixel_window: TODO updates. check-in: de16f341b7 user: andy tags: trunk | |
| 2025-04-29 | ||
| 22:46 | pixel_robot: Started adding OKLAB/OKLCH color spaces. check-in: 65eea726c6 user: andy tags: trunk | |
| 2025-04-21 | ||
| 18:19 | pixel_window: Added sprite_batch's remap shaders to the standard shaders header. check-in: 748eda1442 user: andy tags: trunk | |
Changes
Changes to pixel_robot/TODO.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 
Pixel Robot TODOs:
 
  ✔ Switch from int64_t to int32_t for coordinates? @done (25-03-26 12:43)
     I think int32_t will play nicer with QuickJS, and is big enough for
     anything we want to do. It would also allow us to pack coordinates
     into a struct, which (on x86-64) could be passed in a single register
     instead of two.
  ☐ Inconsistency between go_to and frames.go_to...?
     Currently, the global next/go_to functions are *not* just wrappers around
     the Image methods; they are defined in src/js.cpp and call the functions
     in sketch.hpp. But we also have a go_to method on Image (frame_stack)
     which is separate.
     Note that frame_stack does not have a `next()` method, so we implement
 | > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 
Pixel Robot TODOs:
 
  ✔ Switch from int64_t to int32_t for coordinates? @done (25-03-26 12:43)
     I think int32_t will play nicer with QuickJS, and is big enough for
     anything we want to do. It would also allow us to pack coordinates
     into a struct, which (on x86-64) could be passed in a single register
     instead of two.
  ☐ OKLCH/LAB color space? <https://bottosson.github.io/posts/oklab/>
    ☐ rgb_color::mix() for OKLCH space
    ☐ color similarity for OKLAB/OKLCH spaces (in rgb_color.cpp)
  ☐ Inconsistency between go_to and frames.go_to...?
     Currently, the global next/go_to functions are *not* just wrappers around
     the Image methods; they are defined in src/js.cpp and call the functions
     in sketch.hpp. But we also have a go_to method on Image (frame_stack)
     which is separate.
     Note that frame_stack does not have a `next()` method, so we implement
 | 
| ︙ | ︙ | |||
| 925 926 927 928 929 930 931 932 933 934 935 936 937 938 | 
        See also
           <http://nerdlypleasures.blogspot.com/2023/03/the-saga-of-color-brown-in-early-years.html?m=1>
     ☐ EGA 
        EGA uses the same 16-color palette as CGA, but has access to a 64-color
        palette. Artifact colors are not possible.
  Documentation:
    ✔ Convert documentation to LuaLaTeX @done (24-11-22 09:56)
       (in progress)
       ✔ Main conversion @done (24-10-29 09:19)
 | > > | 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 | 
        See also
           <http://nerdlypleasures.blogspot.com/2023/03/the-saga-of-color-brown-in-early-years.html?m=1>
     ☐ EGA 
        EGA uses the same 16-color palette as CGA, but has access to a 64-color
        palette. Artifact colors are not possible.
     ☐ 12-bit <https://iamkate.com/data/12-bit-rainbow/>
  Documentation:
    ✔ Convert documentation to LuaLaTeX @done (24-11-22 09:56)
       (in progress)
       ✔ Main conversion @done (24-10-29 09:19)
 | 
| ︙ | ︙ | 
Changes to pixel_robot/include/pixel_robot/rgb_color.hpp.
| ︙ | ︙ | |||
| 134 135 136 137 138 139 140 | 
            RGBA = RGB,
            HSL,
            HSV,
            XYZ, 
            LAB,
            YIQ,
            YUV,
 | | > > > > > > | 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | 
            RGBA = RGB,
            HSL,
            HSV,
            XYZ, 
            LAB,
            YIQ,
            YUV,
            YPBPR,
            OKLAB,
            OKLCH
        };
        void to_color_space(color_space space, float& x, float& y, float& z) const
        {
            switch(space) {
                case RGB:  x = r/255.0; y = g/255.0; z = b/255.0; return;
                case HSL: to_hsl(x,y,z); return;
                case HSV: to_hsv(x,y,z); return;
                case XYZ: to_xyz(x,y,z); return;
                case LAB: to_lab(x,y,z); return;
                case YIQ: to_yiq(x,y,z); return;
                case YUV: to_yuv(x,y,z); return;
                case YPBPR: to_YPbPr(x,y,z); return;
                case OKLAB: to_OKLAB(x,y,z); return;
                case OKLCH: to_OKLCH(x,y,z); return;
            }
        }
        /* to_*(c, x, y, z)
           Converts a RGB color to various other color spaces.
        */
        void to_hsl(float& h, float& s, float& l) const;
        void to_hsv(float& h, float& s, float& v) const;
        void to_xyz(float& x, float& y, float& z) const;
        void to_lab(float& l, float& a, float& b) const;
        void to_YPbPr(float& Y, float& Pb, float& Pr) const;
        void to_OKLAB(float& L, float& a, float& b) const;
        void to_OKLCH(float& L, float& C, float& H) const;
        /* to_yiq(y,i,q)
           Converts the color to the YIQ color space, used by NTSC (analog) 
           televisions. YIQ is useful for generating colors/gradients which 
           *look like* they came from a TV in the 1980s. 
           NOTE: After the transformation, the Y,I,Q components will be in these
 | 
| ︙ | ︙ | |||
| 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | 
        static rgb_color from_hsv(float h, float s, float v);
        static rgb_color from_hsl(float h, float s, float l);
        static rgb_color from_xyz(float x, float y, float z);
        static rgb_color from_lab(float l, float a, float b);
        static rgb_color from_yiq(float y, float i, float q);
        static rgb_color from_yuv(float y, float u, float v);
        static rgb_color from_YPbPr(float Y, float Pb, float Pr);
        static rgb_color from_color_space(
            color_space space,
            float x, float y, float z
        )
        {
            switch(space) {
 | > > | 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | 
        static rgb_color from_hsv(float h, float s, float v);
        static rgb_color from_hsl(float h, float s, float l);
        static rgb_color from_xyz(float x, float y, float z);
        static rgb_color from_lab(float l, float a, float b);
        static rgb_color from_yiq(float y, float i, float q);
        static rgb_color from_yuv(float y, float u, float v);
        static rgb_color from_YPbPr(float Y, float Pb, float Pr);
        static rgb_color from_OKLAB(float L, float a, float b);
        static rgb_color from_OKLCH(float L, float C, float h);
        static rgb_color from_color_space(
            color_space space,
            float x, float y, float z
        )
        {
            switch(space) {
 | 
| ︙ | ︙ | |||
| 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | 
                case HSL: return from_hsl(x,y,z);
                case HSV: return from_hsv(x,y,z);
                case XYZ: return from_xyz(x,y,z);
                case LAB: return from_lab(x,y,z);
                case YIQ: return from_yiq(x,y,z);
                case YUV: return from_yuv(x,y,z);
                case YPBPR: return from_YPbPr(x, y, z);
            }
            // This should be unreachable
            return rgb_color::hotpink();
        }
        /* Modifying colors
 | > > | 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | 
                case HSL: return from_hsl(x,y,z);
                case HSV: return from_hsv(x,y,z);
                case XYZ: return from_xyz(x,y,z);
                case LAB: return from_lab(x,y,z);
                case YIQ: return from_yiq(x,y,z);
                case YUV: return from_yuv(x,y,z);
                case YPBPR: return from_YPbPr(x, y, z);
                case OKLAB: return from_OKLAB(x,y,z);
                case OKLCH: return from_OKLCH(x,y,z);
            }
            // This should be unreachable
            return rgb_color::hotpink();
        }
        /* Modifying colors
 | 
| ︙ | ︙ | |||
| 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | 
            Colors ... colors
        )
        {
            static_assert(sizeof...(Colors) > 0,
                "rgb_color::mix() must be passed at least one color parameter");
            if(space == color_space::HSL or space == color_space::HSV) {
                // In HSL/V space, we can't just average the components, because
                // hue is angular. So instead we exploit the fact that HSL/V
                // space can be viewed in 3D-space as a double-cone (HSL) or 
                // cone (HSV). So we can convert all the colors into a 
                // Cartesian 3D space (x,y,z), average there, and then convert
                // back into HSL/V. 
 | > > | 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 | 
            Colors ... colors
        )
        {
            static_assert(sizeof...(Colors) > 0,
                "rgb_color::mix() must be passed at least one color parameter");
            if(space == color_space::HSL or space == color_space::HSV) {
                // TODO: This also for OKLCH.
                // In HSL/V space, we can't just average the components, because
                // hue is angular. So instead we exploit the fact that HSL/V
                // space can be viewed in 3D-space as a double-cone (HSL) or 
                // cone (HSV). So we can convert all the colors into a 
                // Cartesian 3D space (x,y,z), average there, and then convert
                // back into HSL/V. 
 | 
| ︙ | ︙ | |||
| 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 | 
            cs = rgb_color::color_space::YIQ;
        else if(s == "lab")
            cs = rgb_color::color_space::LAB;
        else if(s == "yuv")
            cs = rgb_color::color_space::YUV;
        else if(s == "ypbpr")
            cs = rgb_color::color_space::YPBPR;
        else
            return false;
        return true;
    }
    /* color_similarity
       Names of the different color similarity methods we can use.
    */
    enum class color_similarity
    {
        RGB,
        WEIGHTED,
        CIE76,
        CIE94,
        HSV,
        HSL,
        XYZ,
        YIQ,
        YUV,
 | > > > > | > > | 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 | 
            cs = rgb_color::color_space::YIQ;
        else if(s == "lab")
            cs = rgb_color::color_space::LAB;
        else if(s == "yuv")
            cs = rgb_color::color_space::YUV;
        else if(s == "ypbpr")
            cs = rgb_color::color_space::YPBPR;
        else if(s == "oklab")
            cs = rgb_color::color_space::OKLAB;
        else if(s == "oklch")
            cs = rgb_color::color_space::OKLCH;
        else
            return false;
        return true;
    }
    /* color_similarity
       Names of the different color similarity methods we can use.
    */
    enum class color_similarity
    {
        RGB,
        WEIGHTED,
        CIE76,
        CIE94,
        HSV,
        HSL,
        XYZ,
        YIQ,
        YUV,
        YPBPR,
        OKLAB,
        OKLCH
    };
    inline bool from_string(const std::string& s, color_similarity& cs)
    {
        if(s == "rgb" or s == "rgba") 
            cs = color_similarity::RGB;
        else if(s == "weighted")
 | 
| ︙ | ︙ | |||
| 514 515 516 517 518 519 520 521 522 523 524 525 526 527 | 
            cs = color_similarity::XYZ;
        else if(s == "yiq")
            cs = color_similarity::YIQ;
        else if(s == "yuv")
            cs = color_similarity::YUV;
        else if(s == "ypbpr")
            cs = color_similarity::YPBPR;
        else
            return false;
        return true;
    }
    /* distance(a,b,space)
 | > > > > | 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 | 
            cs = color_similarity::XYZ;
        else if(s == "yiq")
            cs = color_similarity::YIQ;
        else if(s == "yuv")
            cs = color_similarity::YUV;
        else if(s == "ypbpr")
            cs = color_similarity::YPBPR;
        else if(s == "oklab")
            cs = color_similarity::OKLAB;
        else if(s == "oklch")
            cs = color_similarity::OKLCH;
        else
            return false;
        return true;
    }
    /* distance(a,b,space)
 | 
| ︙ | ︙ | 
Changes to pixel_robot/src/rgb_color.cpp.
| ︙ | ︙ | |||
| 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 
#include <pixel_robot/js.hpp>
#include <pixel_robot/resource.hpp>
#include <pixel_robot/rgb_color.hpp>
#include <pixel_robot/sketch.hpp>
namespace probot {
    /* srgb_to_linear(r,g,b)
       Converts a sRGB color (components 0...255) to linear RGB (components
       0...1).
    */
    static void srgb_to_linear(float& r, float& g, float& b)
    {
 | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | 
#include <pixel_robot/js.hpp>
#include <pixel_robot/resource.hpp>
#include <pixel_robot/rgb_color.hpp>
#include <pixel_robot/sketch.hpp>
namespace probot {
    /* OKLAB/OKLCH conversion from/to sRGB
       This code is copied, basically unchanged, from 
            <https://bottosson.github.io/posts/oklab/>
       The original is in the public domain.
    */
    struct Lab {float L; float a; float b;};
    struct RGB {float r; float g; float b;};
    static Lab linear_srgb_to_oklab(RGB c) 
    {
        float l = 0.4122214708f * c.r + 0.5363325363f * c.g + 0.0514459929f * c.b;
        float m = 0.2119034982f * c.r + 0.6806995451f * c.g + 0.1073969566f * c.b;
        float s = 0.0883024619f * c.r + 0.2817188376f * c.g + 0.6299787005f * c.b;
        float l_ = cbrtf(l);
        float m_ = cbrtf(m);
        float s_ = cbrtf(s);
        return {
            0.2104542553f*l_ + 0.7936177850f*m_ - 0.0040720468f*s_,
            1.9779984951f*l_ - 2.4285922050f*m_ + 0.4505937099f*s_,
            0.0259040371f*l_ + 0.7827717662f*m_ - 0.8086757660f*s_,
        };
    }
    static RGB oklab_to_linear_srgb(Lab c) 
    {
        float l_ = c.L + 0.3963377774f * c.a + 0.2158037573f * c.b;
        float m_ = c.L - 0.1055613458f * c.a - 0.0638541728f * c.b;
        float s_ = c.L - 0.0894841775f * c.a - 1.2914855480f * c.b;
        float l = l_*l_*l_;
        float m = m_*m_*m_;
        float s = s_*s_*s_;
        return {
            +4.0767416621f * l - 3.3077115913f * m + 0.2309699292f * s,
            -1.2684380046f * l + 2.6097574011f * m - 0.3413193965f * s,
            -0.0041960863f * l - 0.7034186147f * m + 1.7076147010f * s,
        };
    }
    /* srgb_to_linear(r,g,b)
       Converts a sRGB color (components 0...255) to linear RGB (components
       0...1).
    */
    static void srgb_to_linear(float& r, float& g, float& b)
    {
 | 
| ︙ | ︙ | |||
| 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | 
        srgb_to_linear(red, green, blue);
        Y =  0.299 * red  + 0.587 * green + 0.114 * blue ;
        Pb = 0.564 * (blue - Y);
        Pr = 0.713 * (red - Y);
    }
    rgb_color rgb_color::from_hsv(float h, float s, float v)
    {
        // Assumes h is degrees [0...360); s, v ∈ [0,1] 
        h = std::fmod(h, 360);
        if(s <= 0.0f) {
 | > > > > > > > > > > > > > > > > > > > > | 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | 
        srgb_to_linear(red, green, blue);
        Y =  0.299 * red  + 0.587 * green + 0.114 * blue ;
        Pb = 0.564 * (blue - Y);
        Pr = 0.713 * (red - Y);
    }
    void rgb_color::to_OKLAB(float& L, float& a, float& b) const
    {
        float red = r, green = g, blue = b;
        srgb_to_linear(red, green, blue);
        Lab c = linear_srgb_to_oklab({ float(red), float(green), float(blue) });
        L = c.L;
        a = c.a;
        b = c.b;
    }
    void rgb_color::to_OKLCH(float& L, float& C, float& H) const
    {
        float a, b;
        to_OKLAB(L, a, b);
        C = std::sqrt(a*a + b*b);
        H = std::atan2(b, a);
    }
    rgb_color rgb_color::from_hsv(float h, float s, float v)
    {
        // Assumes h is degrees [0...360); s, v ∈ [0,1] 
        h = std::fmod(h, 360);
        if(s <= 0.0f) {
 | 
| ︙ | ︙ | |||
| 457 458 459 460 461 462 463 | 
            saturate(b),
            255
        };
    }
    rgb_color rgb_color::from_YPbPr(float Y, float Pb, float Pr)
    {
 | < < < < < < > > > > > > > > > > > > > > > > > > | 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 | 
            saturate(b),
            255
        };
    }
    rgb_color rgb_color::from_YPbPr(float Y, float Pb, float Pr)
    {
        float b = Pb / 0.564 + Y; // by = B - Y       B = by - Y
        float r = Pr / 0.713 + Y; // ry = R - Y       R = ry - Y
        float g = Y - 0.299 * (Pr / 0.713) / 0.587 - 0.114 * (Pb / 0.564) / 0.587;
        linear_to_srgb(r, g, b);
        return rgb_color{
            saturate(r),
            saturate(g),
            saturate(b),
            255
        };
    }
    rgb_color rgb_color::from_OKLAB(float L, float a, float b)
    {
        struct RGB c = oklab_to_linear_srgb({L, a, b});
        linear_to_srgb(c.r, c.g, c.b);
        return rgb_color{ saturate(c.r), saturate(c.g), saturate(c.b) };
    }
    rgb_color rgb_color::from_OKLCH(float L, float C, float h)
    {
        Lab c = { 
            L, 
            C * std::cos(h),
            C * std::sin(h)
        };
        return from_OKLAB(c.L, c.a, c.b);
    }
    rgb_color rgb_color::mix(
        color_space space, 
        const std::vector<rgb_color>& colors
    )
    {
        if(colors.empty())
 | 
| ︙ | ︙ |