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 7, 2022
via email
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