Search code examples
colorscolor-spacelossless

Lossless sRGB <-> Lab conversion?


When I convert black (rgb(0,0,0)) to LAB and back again it comes out as 19, 0, 10.

Is there a modified formula that expands the Lab color-space for 1:1 conversion? And if not, are there any other color-spaces that maintain the property of "the same amount of numerical change in these values corresponds to roughly the same amount of visually perceived change"?

I want to run k-means clustering on some images and it works better in Lab space.


Solution

  • The short answer is the code library you are using has bugs.

    And it does not seem to be actively maintained.

    FWIW, when you are looking at values, remember that:

    #000

    sRGB 0,0,0 = linearRGB 0.0,0.0,0.0 = XYZ 0,0,0 = Lab 0,0,0
    (All spaces 0,0,0)

    #FFF

    sRGB 255,255,255 = linearRGB 1.0,1.0,1.0 = XYZ 0.9505,1.0,1.0888 = Lab 100,0,0
    (sRGB is D65, this assumes D65 2° observer)

    #777

    sRGB 119,119,119 = linearRGB 0.1845,0.1845,0.1845 = XYZ 0.1753,0.1845,0.2009 = Lab 50.03,0,0
    (using BruceLindbloom Matrix)

    Pseudocode

    More correct code can be found here:

    https://www.easyrgb.com/en/math.php

    However, this code is NOT javascript — the code on this site is pseudocode, and so it needs to be modified (i.e. you need to use Math.pow and not ^ )

    Python

    There is a good Python based library here, and it is actively maintained:

    https://github.com/colour-science/colour

    MATH

    The actual math and discussion can be found on Bruce Lindbloom's site:

    http://www.brucelindbloom.com/index.html?Math.html

    He also has some color calculators that are JS that might be helpful to you.

    Happy to answer other questions, lemme know...