Leaves of Three Open Source Projects

Check-in [7808fe33aa]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:pixel_robot: Handle OKLCH color mixing.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 7808fe33aa1aba3620ba27c560d284f61777ed9f80f1ec9b5586ac4fd1e82242
User & Date: andy 2025-05-01 23:37:59
Context
2025-05-01
23:38
pixel_robot: Added OKLAB/OKLCH to documentation. check-in: c18fdb3fc6 user: andy tags: trunk
23:37
pixel_robot: Handle OKLCH color mixing. check-in: 7808fe33aa user: andy tags: trunk
2025-04-30
16:03
pixel_robot: Added missing code for OKLAB/OKLCH color distance and similarity. check-in: dd8aeb171c user: andy tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to pixel_robot/include/pixel_robot/rgb_color.hpp.

369
370
371
372
373
374
375

376
377
378
379
380
381
382
                float x, y, z;
                float sx = 0, sy = 0, sz = 0;

                auto to_xyz = 
                    [space](rgb_color c, float &x, float& y, float &z) -> void
                    {
                        float h, s, lv;

                        c.to_color_space(space, h, s, lv);

                        x = s * std::cos( 2 * 3.14159f * h / 360.0f );
                        y = s * std::sin( 2 * 3.14159f * h / 360.0f );
                        z = lv;
                    };








>







369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
                float x, y, z;
                float sx = 0, sy = 0, sz = 0;

                auto to_xyz = 
                    [space](rgb_color c, float &x, float& y, float &z) -> void
                    {
                        float h, s, lv;
                        // `space` will be HSL or HSV
                        c.to_color_space(space, h, s, lv);

                        x = s * std::cos( 2 * 3.14159f * h / 360.0f );
                        y = s * std::sin( 2 * 3.14159f * h / 360.0f );
                        z = lv;
                    };

400
401
402
403
404
405
406








































407
408
409
410
411
412
413
                h = 360 * h / (2 * 3.14159);

                float s = std::sqrt(sx*sx + sy*sy);
                float lv = sz;

                return rgb_color::from_color_space(space, h, s, lv);
            }








































            else {
                // For other color spaces its sufficient to convert into the 
                // color space, compute the average there, and then convert
                // back to RGB space at the end.
                float sx = 0, sy = 0, sz = 0;
                float x, y, z;








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
                h = 360 * h / (2 * 3.14159);

                float s = std::sqrt(sx*sx + sy*sy);
                float lv = sz;

                return rgb_color::from_color_space(space, h, s, lv);
            }
            else if(space == color_space::OKLCH) {
                // We have to do the same thing for OKLCH, as it's a cylindrical
                // color space like HSL/HSV.
                float x, y, z;
                float sx = 0, sy = 0, sz = 0;

                auto to_xyz = 
                    [](rgb_color c, float &x, float& y, float &z) -> void
                    {
                        float OK_L, OK_c, OK_h;                        
                        c.to_color_space(OKLCH, OK_L, OK_c, OK_h);

                        x = OK_c * std::cos( 2 * 3.14159f * OK_h / 360.0f );
                        y = OK_c * std::sin( 2 * 3.14159f * OK_h / 360.0f );
                        z = OK_L;
                    };

                (
                    ( to_xyz(colors, x, y, z),
                      sx += x,
                      sy += y,
                      sz += z
                    ),
                    ...
                );

                // Average
                sx /= sizeof...(colors);
                sy /= sizeof...(colors);
                sz /= sizeof...(colors);

                // Convert back to OKLCH
                float OK_h = std::fmod( std::atan2(sy, sx) + 2 * 3.14159 , 2 * 3.14159) ;
                OK_h = 360 * OK_h / (2 * 3.14159);

                float OK_c = std::sqrt(sx*sx + sy*sy);
                float OK_L = sz;

                return rgb_color::from_color_space(OKLCH, OK_L, OK_c, OK_h);
            }
            else {
                // For other color spaces its sufficient to convert into the 
                // color space, compute the average there, and then convert
                // back to RGB space at the end.
                float sx = 0, sy = 0, sz = 0;
                float x, y, z;