I´m not sure if i understood. Tween means changing hues to avoid large difference ? Don´t know the meaning of "tween" in english. Google returned me it means 'tweak', but..still don´t understood :icon_mrgreen:. If it is, the HSV, CieLCH colorspaces already have some If conditions to deal with the Hue direction problem. The direction is checked using thresholds. To fix problems of hue directions (so, the opposed signs) is necessary to compute the "a" and "b" factor from CieLab colorspace (Or as i did directly from CieLCH).
For example, on CieLCh colorspace, Hue is determined by the following equation:
Hue = Atan (b/a), where b is the "Bfactor" from CieLab and "a" the "Afactor" from CieLab
"a" and "b" are achieved from the X,Y, Z values from the XYZ colorspace using these formulas:
a = (X-Y)*500
b = (Y-Z)*200
whereas, Luma = Y*116-16
Also, the value of "a" and "b' can be found using the CieLCh colorspace with the following formulas:
a = cos(hue)*chroma
b = sin(hue)*chroma
The correct way to find "a" depends on the colorspce you are working with. I made a variation of CieLCH that uses XYZ values directly rather then having to migrate in between each colorspace to find the very same value.
The final values of X, Y and Z are calculated from a multiplication of Red, Green and Blue with the proper matrices for AdobeRGB, sRGb etc. and after a IF condition to check some thresholds.
The If condition is in the form of:
If X > (216/24389)
X = X^1/3
Else
X = X*(841/108) + (16/116)
Endif
The same conditions are used for Y and Z
The complete pseudo-code to transform RGB to CieLCh is this:
Red = Red/255 ; The integer colors must be normalized 1st. So, divide each color (Integer) by 255.
Green = Red/255
Blue = Red/255
TmpRed = Red*Kfactor
TmpGreen = Green*Kfactor
TmpBlue = Blue*Kfactor
where "Kfactor" is computed using the gamma and offset adjustments . (If needed i post the formula to achieve "Kfactor" for you).
X = (TmpRed*Matrix.RedM1 + TmpGreen*Matrix.GreenM2 + TmpBlue+Matrix.BlueM3) / WhiteReferenceX
Y = (TmpRed*Matrix.RedM4 + TmpGreen*Matrix.GreenM5 + TmpBlue+Matrix.BlueM6) / WhiteReferenceY
Z = (TmpRed*Matrix.RedM7 + TmpGreen*Matrix.GreenM8 + TmpBlue+Matrix.BlueM9) / WhiteReferenceZ
where:
Matrix.RedM1, Matrix.RedM2, Matrix.RedM3 ... are the tristimulus values for XYZ colorspace used in Adobe1998, sRGB etc. Example, for sRGB, the values are:
Matrix.RedM1 Matrix.GreenM2 Matrix.BlueM3
0.4124564 0.3575761 0.1804375
Matrix.RedM4 Matrix.GreenM5 Matrix.BlueM6
0.2126729 0.7151522 0.0721749
Matrix.RedM7 Matrix.GreenM8 Matrix.BlueM9
0.0193339 0.1191920 0.9503041
WhiteReferenceX, WhiteReferenceY, WhiteReference| are the white references for observers in 2º or 10º degree described in CIE convention. For example:
For D65, 2º observer (Daylight, sRGB, Adobe-RGB)
WhiteReferenceX = 95.047 . Btw: WhiteReference is nothing more then the simple sum of the matrice values multiplied by 100. Ex: (0.4124564+0.3575761+0.1804375)*100
WhiteReferenceY = 100
WhiteReferenceZ = 108.883
If X > (216/24389)
X = X^1/3
Else
X = X*(841/108) + (16/116)
Endif
If Y > (216/24389)
Y = Y^1/3
Else
Y = Y*(841/108) + (16/116)
Endif
If Z > (216/24389)
Z = Z^1/3
Else
Z = Z*(841/108) + (16/116)
Endif
; The above X,Y,Z are the same ones found in XYZ colorspace. I then convert them to lab and after it to hue/chroma (In fact, i make it directly on the same function) , but, below are the necessary equation used, in case you want only the CieLab convertion
; retrieve a, b and Luminance
Luma = Y*116-16
a = (X-Y)*500
b = (Y-Z)*200
and finally, achieve Hue and chroma from "a" and "b"
Chroma = sqrt(a^2+b^2)
Hue = atan (b/a)
This is the pseudo-code to convert from RGB to CieLCH. Here i´m using gamma adjustment (The "Kfactor" i created to make those equations easier and the code faster to process) and white reference to achieve a higher level of accuracy and making the image looks better according to a chosen colorspace (Adobe RGB 1998, sRGB, HDTV etc)
The whole convertion itself is not that complicated after i simplified the equations. The main problem relies on the way those conversion works. CieLab and other perceptual colorspaces have something called color discontinuity that is nothing more nothing less then a failure on the original equations (I call it simply as: bulls*

)
bruce lindbloom make an effort to fix the error here:
http://www.brucelindbloom.com/LContinuity.htmlIn fact, on a mathematical point of view, he was correct, but conceptually, it still don´t fixes the problem with those colorspaces.
For example, using Y*116-16 will necessarily lead to an error on the way color is perceived, since we are generating a "gap" of 16 values on the equations (I have no idea how to fix that properly. So...i´m using Bruce´s suggestion)
One way to fix those colorspaces is create a new one as i did last year. The colorspace i designed is spherical, using all 3 dimensions as the vertices for Red, Green and Blue without any usage of those "gaps". The usage of a spherical colorspace seems more logical and accurate since "color' is nothing more then the form that our eyes (brain, in fact), perceive an eletromagnetic wave after passing through our eyes and cells responsible for color perception. Eletromagnetic waves travels in space in all directions, at the same speed. Considering a tridimensional space, then we can then represent each x,y,z axis as Red, Green and Blue and work with them proportionally (Spherically), instead using those crazy equations actually used. It is a variation of a TSL colorspace as i mentioned earlier. I succeeded to create a formula to convert back, but still have problems, because i have to find the limits the resultant values and after a while i gave up trying. maybe eventually i´ll restart that work.