Created
September 29, 2017 10:47
-
-
Save tcoupin/980a0c66442f58dfcdc7184a3d446b3a to your computer and use it in GitHub Desktop.
PL/Pgsql function to convert color in HSL to RGB hex code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CREATE OR REPLACE FUNCTION hsl2rvb_hexa(hue float, sat float, light float) | |
| RETURNS text AS $$ | |
| declare | |
| c float := sat*light; | |
| tp float := (hue/60.0); | |
| x float := c*((1-abs((tp::int)%2-1))::float); | |
| m float := light-c; | |
| rp float; | |
| vp float; | |
| bp float; | |
| r int; | |
| v int; | |
| b int; | |
| BEGIN | |
| case | |
| when (tp>=0 and tp<1) then | |
| rp=c; | |
| vp=x; | |
| bp=0.; | |
| when (tp>=1 and tp<2) then | |
| rp=x; | |
| vp=c; | |
| bp=0.; | |
| when (tp>=2 and tp<3) then | |
| rp=0.; | |
| vp=c; | |
| bp=x; | |
| when (tp>=3 and tp<4) then | |
| rp=0.; | |
| vp=x; | |
| bp=c; | |
| when (tp>=4 and tp<5) then | |
| rp=x; | |
| vp=0.; | |
| bp=c; | |
| when (tp>=5 and tp<6) then | |
| rp=c; | |
| vp=0.; | |
| bp=x; | |
| else | |
| rp=0.; | |
| vp=0.; | |
| bp=0.; | |
| end case; | |
| r=((rp+m)*255)::int; | |
| v=((vp+m)*255)::int; | |
| b=((bp+m)*255)::int; | |
| return format('#%02s%02s%02s',to_hex(r),to_hex(v),to_hex(b)); | |
| END; | |
| $$ LANGUAGE plpgsql; |
mvlijmen
commented
Nov 4, 2022
via email
Hi Thibault,
Thanks for your reply. I can imagine that something you made 5 years ago is still at the top of your brain.
Hue should be indeed 0-360 degrees. Saturation and lightness should be 1-100 percent. Is that what you mean with [0;1]?
I’m looking for a way to select colors that are recognizable for the colorblind, like for instance secondary colors. The HSL colorspace is good to choose from but you’ll need the HEX representation to get it to work in an application.
Kind regards,
Michiel
… Op 4 nov. 2022, om 08:37 heeft Thibault Coupin ***@***.***> het volgende geschreven:
@tcoupin commented on this gist.
Hi, I don't use this since a lot of time!
But I think think that:
hue must be in [0;360]
sat must be in [0;1]
light must be in [0;1]
=> https://en.wikipedia.org/wiki/HSL_and_HSV#To_RGB <https://en.wikipedia.org/wiki/HSL_and_HSV#To_RGB>
—
Reply to this email directly, view it on GitHub <https://gist.github.com/980a0c66442f58dfcdc7184a3d446b3a#gistcomment-4358150> or unsubscribe <https://github.com/notifications/unsubscribe-auth/A37RL2ELFKHXOKVTZA3XFI3WGS4MXBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFQKSXMYLMOVS2I5DSOVS2I3TBNVS3W5DIOJSWCZC7OBQXE5DJMNUXAYLOORPWCY3UNF3GS5DZVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVA4DCMBWGY2TGNNHORZGSZ3HMVZKMY3SMVQXIZI>.
You are receiving this email because you commented on a thread.
Triage notifications on the go with GitHub Mobile for iOS <https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675> or Android <https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
Author
yes 25% = 0.25
but take care of conversion between int and float. You need to cast your int to float and then divided it by 100.0
Hi Thibault,
Great, thanks. Select hsl2rvb_hexa(0,(0.93),(0.93)) gives the expected conversion #ed1111 (with or without a cast to float).
The only thing that puzzles me is that above 93% (0.94 - 1.0) I get one digit hex output with spaces, like #f0 e e
Sorry to be a pain i.t.a but I hope you can help me.
Kind regards,
Michiel
… Op 4 nov. 2022, om 09:44 heeft Thibault Coupin ***@***.***> het volgende geschreven:
@tcoupin commented on this gist.
yes 25% = 0.25
but take care of conversion between int and float. You need to cast your int to float and then divided it by 100.0
—
Reply to this email directly, view it on GitHub <https://gist.github.com/980a0c66442f58dfcdc7184a3d446b3a#gistcomment-4358220> or unsubscribe <https://github.com/notifications/unsubscribe-auth/A37RL2AN2FUCMI6DGJLCQ4DWGTEHHBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFQKSXMYLMOVS2I5DSOVS2I3TBNVS3W5DIOJSWCZC7OBQXE5DJMNUXAYLOORPWCY3UNF3GS5DZVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVA4DCMBWGY2TGNNHORZGSZ3HMVZKMY3SMVQXIZI>.
You are receiving this email because you commented on a thread.
Triage notifications on the go with GitHub Mobile for iOS <https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675> or Android <https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
Hi Thibault,
I added LPAD() to the last line of your script which solved for me the missing zero’s. The function is working fine now.
Thanks.
Kind regards,
Michiel
return format('#%02s%02s%02s',LPAD(to_hex(r),2,'0'),LPAD(to_hex(v),2,'0'),LPAD(to_hex(b),2,'0'));
… Op 4 nov. 2022, om 09:44 heeft Thibault Coupin ***@***.***> het volgende geschreven:
@tcoupin commented on this gist.
yes 25% = 0.25
but take care of conversion between int and float. You need to cast your int to float and then divided it by 100.0
—
Reply to this email directly, view it on GitHub <https://gist.github.com/980a0c66442f58dfcdc7184a3d446b3a#gistcomment-4358220> or unsubscribe <https://github.com/notifications/unsubscribe-auth/A37RL2AN2FUCMI6DGJLCQ4DWGTEHHBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFQKSXMYLMOVS2I5DSOVS2I3TBNVS3W5DIOJSWCZC7OBQXE5DJMNUXAYLOORPWCY3UNF3GS5DZVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVA4DCMBWGY2TGNNHORZGSZ3HMVZKMY3SMVQXIZI>.
You are receiving this email because you commented on a thread.
Triage notifications on the go with GitHub Mobile for iOS <https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675> or Android <https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
tp must not be converted to int before doing modulo. Instead use this modulo function https://stackoverflow.com/a/53487651/289827
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment