Qlik RGB() to HSL() to RGB() / ARGB()
Robert Svebeck
Driving Responsible AI Implementation in Region Stockholm / Karolinska University Hospital
For a project I recently had a special RGB color code and I needed to make it just slightly brighter and more colorful. Instead of trying to adjust the Red, Green and Blue values until I got what I needed I started searching for a way to convert my RGB color code to a HSL color code. I didn't find any way!?!
Then the next day, I had a HSL color code but I needed to make it a little bit transparent. I searched for a solution but couldn't find one for that either!?!
In case you are not aware - to make a color transparent in Qlik, you can use the ARGB() function. So I needed to convert my HSL color to RGB and then change the transparency with ARGB().
Since I found no solution - I decided to solve it myself. I googled a little on how this kind of conversion is done in other languages and translated the code back into Qlik Script.
I will not bother you with the maths, and why it is working. Plenty of explenations out there in case you are interested.
Here is the solutions in Qlik
Convert RGB() to HSL()
sub rgb2hsl(r, g, b) let r = r/255; let g = g/255; let b = b/255; let hue=0; let saturation=0; let luminosity=0; let max = rangemax(r, g, b); let min = rangemin(r, g, b); let delta = max - min; if delta > 0 then if max=r then let segment = (g - b) / delta; let shift = 0 / 60; if segment < 0 then shift = 360 / 60; end if hue = segment + shift; end if if max=g then let segment = (b - r) / delta; let shift = 120 / 60; hue = segment + shift; end if if max=b then let segment = (r - g) / delta; let shift = 240 / 60; hue = segment + shift; end if let hue = hue * 60 / 360; let luminosity = (max + min) / 2; let saturation = delta/(1-fabs(2*luminosity-1)); end if end Sub
To use this sub, just run it like this:
call rgb2hsl(YourRed,YourGreen,YourBlue);
Where YourRed,YourGreen,YourBlue are any value between 0-255.
The sub "returns" 3 variables: hue, saturation and luminosity. (I wish subs could actually return variables in Qlik....)
Convert HSL() to RGB()
To do the opposite, here is how:
sub hsl2rgb(h,s,l) let r=l; let g=l; let b=l; let v = if (l <= 0.5,l * (1.0 + s),l + s - l * s); if v > 0 then let m = l + l - v; let sv = (v - m ) / v; let h = h * 6; let sextant = floor(h); let fract = frac(h); let vsf = v * sv * fract; let mid1 = m + vsf; let mid2 = v - vsf; if sextant = 0 then let r = v; let g = mid1; let b = m; elseif sextant = 1 then let r = mid2; let g = v; let b = m; elseif sextant = 2 then let r = m; let g = v; let b = mid1; elseif sextant = 3 then let r = m; let g = mid2; let b = v; elseif sextant = 4 then let r = mid1; let g = m; let b = v; elseif sextant = 5 then let r = v; let g = m; let b = mid2; end if end if let Red = round(r * 255); let Green = round(g * 255); let Blue = round(b * 255); end sub
Run the sub like this:
call hsl2rgb(YourHue,YourSaturation,YourLuminosity);
Where YourHue, YourSaturation and YourLuminosity are any value between 0 and 1.
The sub returns 3 variables: Red, Green and Blue.
Then you can just use RGB(Red,Green,Blue) to show the color, or ARGB(100,Red,Green,Blue) to make a transparent version of that color.
The code above is just quickly done - and I am sure many of you out there will know ways to improve it. I intentionally kept is rather "long" so that it is easier to understand.
Let me know if you can convert this code into a function variable instead of using Subs, so it can be used in an expression instead - would be a great improvement!
Thanks for reading,
Have a nice day,
Robert
--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
Here is a good advice from Luis Felipe Tensini :
"We can upgrade this explanation and get HEX value for any color using num(COLOR, '(HEX)')."
=Num(Blue(), '(HEX)') -> This results in 'FF000080'.
Data Specialist | Business Intelligence | Pre-Sales
3 年Post to save, great Robert Svebeck
Qurious Solutions | Assisting our clients with data driven decision making | Qlik Partner Ambassador
3 年Looks great! I was using similar approach but based on variables in Ui when I built a simple colour selection app https://community.qlik.com/t5/QlikView-Documents/TriQs-How-to-select-Colours-for-Qlik-App-Colours-v1-0-app/ta-p/1480661
Qlik Partner Ambassador | MBA Gest?o de Projetos | Os melhores insights com Qlik e Alfabetiza??o de Dados
3 年We can upgrade this explanation and get HEX value for any color using Num(COLOR, '(HEX)'). Try it: =Num(Blue(), '(HEX)') PS.: This results in 'FF000080'.
Data Cathedral Architect, Chief Question Officer
3 年So cool I need to wear shades ??. You just keep proving that math just might survive and that is strong the world were wrong ... yes you will need math when you grow you.