Last active
May 18, 2022 04:52
-
-
Save ashleysommer/24de0bada57c7ce93ed613e1704d90f6 to your computer and use it in GitHub Desktop.
AWK RGB to HSL conversion
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
awk -F "[, ]+" '\ | |
function abs(num) { | |
if (num < 0.0) | |
return num*-1.0 | |
return num | |
} | |
function find_min(num1, num2, num3){ | |
if (num1 < num2) | |
a=num1 | |
else | |
a=num2 | |
if (a < num3) | |
return a | |
return num3 | |
} | |
# Returns maximum number | |
function find_max(num1, num2, num3){ | |
if (num1 > num2) | |
a=num1 | |
else | |
a=num2 | |
if (a > num3) | |
return a | |
return num3 | |
} | |
# Main function | |
function to_hsl(Rd, Gd, Bd){ | |
Rf=Rd/255.0 | |
Gf=Gd/255.0 | |
Bf=Bd/255.0 | |
# Find minimum number | |
m=find_min(Rf, Gf, Bf) | |
# Find maximum number | |
M=find_max(Rf, Gf, Bf) | |
chroma=M-m | |
L=(M+m)/2.0 | |
if (chroma == 0.0) { | |
H=0.0 | |
S=0.0 | |
} else { | |
if (M == Bf) | |
Hprime=((Rf-Gf)/chroma)+4.0 | |
else if (M == Gf) | |
Hprime=((Bf-Rf)/chroma)+2.0 | |
else | |
Hprime=((Gf-Bf)/chroma)%6.0 | |
H=Hprime*60.0 | |
S=chroma/(1.0-abs((2.0*L)-1.0)) | |
} | |
print H, S, L | |
} | |
# Script execution starts here | |
{ | |
to_hsl($1*1.0, $2*1.0, $3*1.0) | |
}' |
ask -F "[, ]+" '\
function abs(num) {
if (num < 0.0)
return num*-1.0
return num
}
# Main function
function rotate_angle(A, R){
R=R%32768.0
A=abs(A)
amount=1.0+((R/32767.0) * 358.0)
A=(A+amount)%360.0
print A
}
# Script execution starts here
{
rotate_angle($1*1.0, $2*1.0)
}'
awk -F "[,]+" '\
function add_offset(S,D,R){
R=R%32768.0
amount=((R/32767.0)*D*2.0)-D
print amount
print S+amount
}
# Script execution starts here
{
add_offset($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
readarray -td, a <<<"$(_rgb_to_hsl($1, $2, $3)),"; unset 'a[-1]';