Convert Python Code for RGB to CIE Lab

Windows specific forum
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Convert Python Code for RGB to CIE Lab

Post by IdeasVacuum »

I have tried to convert this Python code:
https://gist.github.com/manojpandey/f5e ... ebebaf66ae

.... but my result is a mile out, tested on: https://www.nixsensor.com/free-color-converter

Code: Select all

Procedure.s RgbToCieLab(iRed.i, iGrn.i, iBlu.i)
;#---------------------------------------------
;Python Code: https://gist.github.com/manojpandey/f5ece715132c572c80421febebaf66ae
;Converter: https://www.nixsensor.com/free-color-converter
Protected dRgbRed.d, dRgbGrn.d, dRgbBlu.d
Protected dXyzRed.d, dXyzGrn.d, dXyzBlu.d
Protected dLabL.d, dLabA.d, dLabB.d
Protected sXyzRed.s, sXyzGrn.s, sXyzBlu.s
Protected sColour.s

               dRgbRed = (iRed / 255)
               dRgbGrn = (iGrn / 255)
               dRgbBlu = (iBlu / 255)

               If dRgbRed > 0.04045
                         dRgbRed = Pow( ((dRgbRed + 0.055) / 1.055), 2.4 )
               Else
                         dRgbRed = dRgbRed / 12.92
                         dRgbRed = dRgbRed * 100
               EndIf

               If dRgbGrn > 0.04045
                         dRgbGrn = Pow( ((dRgbGrn + 0.055) / 1.055), 2.4 )
               Else
                         dRgbGrn = dRgbGrn / 12.92
                         dRgbGrn = dRgbGrn * 100
               EndIf

               If dRgbBlu > 0.04045
                         dRgbBlu = Pow( ((dRgbBlu + 0.055) / 1.055), 2.4 )
               Else
                         dRgbBlu = dRgbBlu / 12.92
                         dRgbBlu = dRgbBlu * 100
               EndIf

	           dXyzRed = (0.4124240 * dRgbRed) + (0.357579 * dRgbGrn) + (0.1804640 * dRgbBlu) ;0 to 0.9642
	           dXyzGrn = (0.2126560 * dRgbRed) + (0.715158 * dRgbGrn) + (0.0721856 * dRgbBlu) ;0 to 1.0000
	           dXyzBlu = (0.0193324 * dRgbRed) + (0.119193 * dRgbGrn) + (0.9504440 * dRgbBlu) ;0 to 0.8252

               sXyzRed = StrD(dXyzRed, 4)
               sXyzGrn = StrD(dXyzGrn, 4)
               sXyzBlu = StrD(dXyzBlu, 4)

               dXyzRed = ValD(sXyzRed)
               dXyzGrn = ValD(sXyzGrn)
               dXyzBlu = ValD(sXyzBlu)

               ;Observer= 2°, Illuminant= D65
               dXyzRed = (dXyzRed / 95.0470)        ;ref_X =  95.047
               dXyzGrn = (dXyzGrn / 100.000)        ;ref_Y = 100.000
               dXyzBlu = (dXyzBlu / 108.883)        ;ref_Z = 108.883

               If(dXyzRed > 0.008856)

                         dXyzRed = Pow(dXyzRed, 0.3333333333333333)
               Else
                         dXyzRed = (7.787 * dXyzRed) + (16 / 116)
               EndIf

               If(dXyzGrn > 0.008856)

                         dXyzGrn = Pow(dXyzGrn, 0.3333333333333333)
               Else
                         dXyzGrn = (7.787 * dXyzGrn) + (16 / 116)
               EndIf

               If(dXyzBlu > 0.008856)

                         dXyzBlu = Pow(dXyzBlu, 0.3333333333333333)
               Else
                         dXyzBlu = (7.787 * dXyzBlu) + (16 / 116)
               EndIf

               Debug "X-->" + StrD(dXyzRed,4) + "<--37.75"
               Debug "Y-->" + StrD(dXyzGrn,4) + "<--34.52"
               Debug "Z-->" + StrD(dXyzBlu,4) + "<--24.41"

               dLabL = (116 * dXyzGrn) - 16            ;0 to 100
               dLabA = 500 * (dXyzRed - dXyzGrn)       ;-128 to 128
               dLabB = 200 * (dXyzGrn - dXyzBlu)       ;-128 to 128

               sColour = "CIE Lab: " + StrD(dLabL,4) + "," + StrD(dLabA,4) + "," + StrD(dLabB,4)
Debug sColour ;65.37, 16.81, 18.80
               ProcedureReturn(sColour)

EndProcedure

RgbToCieLab(199, 147, 126)

End
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4941
Joined: Sun Apr 12, 2009 6:27 am

Re: Convert Python Code for RGB to CIE Lab

Post by RASHAD »

Approximate solution :D

Code: Select all

    color = RGB(100, 147, 126)
    
    
    value0.f = Red(color)
    value1.f = Green(color)
    value2.f = Blue(color)
    
    value0 = ((value0 + 0.055) / 1.055) * 2.4
    value1 = ((value1 + 0.055) / 1.055) * 2.4
    value2 = ((value2 + 0.055) / 1.055) * 2.4
    
    X.f = value0 * 0.4124 + value1 * 0.3576 + value2 * 0.1805
    Y.f = value0 * 0.2126 + value1 * 0.7152 + value2 * 0.0722
    Z.f = value0 * 0.0193 + value1 * 0.1192 + value2 * 0.9505
    
    XYZ0.f = Round(X,#PB_Round_Up )
    XYZ1.f = Round(Y,#PB_Round_Up )
    XYZ2.f = Round(Z,#PB_Round_Up )


    XYZ0 = XYZ0 / 95.047 
    XYZ1 = XYZ1 / 100.0 
    XYZ2 = XYZ2 / 108.883
    
    L = (116 * XYZ1) - 16
    a = 500 * (XYZ0 - XYZ1)
    b = 200 * (XYZ1 - XYZ2)

    Lab0 = Round(L,#PB_Round_Up )/6
    Lab1 = Round(a,#PB_Round_Up )/6
    Lab2 = Round(b,#PB_Round_Up )/6
    
    sColour.s = "CIE Lab: " + StrD(lab0,4) + "," + StrD(lab1,4) + "," + StrD(lab2,4)
    Debug sColour
Egypt my love
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Convert Python Code for RGB to CIE Lab

Post by IdeasVacuum »

Hi Rashad, always good to hear from you.

Well, close but no cigar :mrgreen:

For colour conversions I can usually find C code which is easy to convert to PB, but this Python code is all I could find.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
JHPJHP
Addict
Addict
Posts: 2250
Joined: Sat Oct 09, 2010 3:47 am

Re: Convert Python Code for RGB to CIE Lab

Post by JHPJHP »

Hi IdeasVacuum,

See Windows Services & Other Stuff\Other_Stuff\ColorConversion\Convert\ConvertCIELAB.pb

NB*: ColorConversion folder includes numerous other examples.
Last edited by JHPJHP on Thu Jan 12, 2023 3:13 am, edited 1 time in total.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Convert Python Code for RGB to CIE Lab

Post by IdeasVacuum »

Thank you JHPJHP - With Illuminant @ D65 and reference angle @ 2 degrees, the result is perfect 8)
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply