Skip to content

Instantly share code, notes, and snippets.

@ashleysommer
Created May 18, 2022 03:57
Show Gist options
  • Save ashleysommer/5276c1137b45f9208b059e5cfd544966 to your computer and use it in GitHub Desktop.
Save ashleysommer/5276c1137b45f9208b059e5cfd544966 to your computer and use it in GitHub Desktop.
AWK HSL to RGB conversion
awk -F "[, ]+" '\
# Returns minimum number
function abs(num) {
if (num < 0.0)
return num*-1.0
return num
}
# Main function
function to_rgb(H, S, L){
H=H%360.0
S=abs(S)
if (S < 0.0) S=0.0
if (S > 1.0) S=1.0
L=abs(L)
if (L < 0.0) L=0.0
if (L > 1.0) L=1.0
C=(1.0-abs((2.0*L)-1.0))*S
X=C*(1.0-abs(((H/60.0)%2.0)-1.0))
m=L-(C/2.0)
if (H >= 300.0) {
Rp=C; Gp=0.0; Bp=X
} else if (H >= 240.0) {
Rp=X; Gp=0.0; Bp=C
} else if (H >= 180.0) {
Rp=0.0; Gp=X; Bp=C
} else if (H >= 120.0) {
Rp=0.0; Gp=C; Bp=X
} else if (H >= 60.0) {
Rp=X; Gp=C; Bp=0.0
} else {
Rp=C; Gp=X; Bp=0.0
}
R=(Rp+m)*255.0
G=(Gp+m)*255.0
B=(Bp+m)*255.0
print int(R+0.5), int(G+0.5), int(B+0.5)
}
# Script execution starts here
{
to_rgb($1*1.0, $2*1.0, $3*1.0)
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment