HSL and HSV are now fully working!

This commit is contained in:
2024-12-11 21:38:27 -05:00
parent 9c855138b3
commit 3fecee0dce
2 changed files with 70 additions and 64 deletions
+10
View File
@@ -34,11 +34,21 @@ maybe these ideas branch off into separate projects themselves, but:
## Conversion Sources ## Conversion Sources
Hex<->RGB: Hex<->RGB:
https://en.wikipedia.org/wiki/Web_colors https://en.wikipedia.org/wiki/Web_colors
RGB<->CMYK: RGB<->CMYK:
https://www.101computing.net/cmyk-to-rgb-conversion-algorithm/ https://www.101computing.net/cmyk-to-rgb-conversion-algorithm/
https://thecolorsmeaning.com/rgb-to-cmyk/ https://thecolorsmeaning.com/rgb-to-cmyk/
RGB<->CMY: RGB<->CMY:
http://colormine.org/convert/rgb-to-cmy http://colormine.org/convert/rgb-to-cmy
RGB<->HSL:
https://www.baeldung.com/cs/convert-color-hsl-rgb
+58 -62
View File
@@ -50,11 +50,11 @@ def main():
# HANDLE HSL INPUT # HANDLE HSL INPUT
if args.hsl : if args.hsl :
handleHSVorHSL(color, True) handleHSVorHSL(color, 'hsl')
# HANDLE HSV INPUT # HANDLE HSV INPUT
if args.hsv : if args.hsv :
handleHSVorHSL(color, False) handleHSVorHSL(color, 'hsv')
@@ -71,11 +71,11 @@ def handleHex(color) :
print('convert hex: ', hexCode, '\n') print('convert hex: ', hexCode, '\n')
rgbValues = hexToRGB(hexCode) rgbValues = HEXtoRGB(hexCode)
cmyValues = rgbToCMY(rgbValues) cmyValues = RGBtoCMY(rgbValues)
cmykValues = rgbToCMYK(rgbValues) cmykValues = RGBtoCMYK(rgbValues)
hslValues = rgbToHSVorHSL(rgbValues, True) hslValues = RGBtoHSVorHSL(rgbValues, 'hsl')
hsvValues = rgbToHSVorHSL(rgbValues, False) hsvValues = RGBtoHSVorHSL(rgbValues, 'hsv')
print('RGB: ', rgbValues) print('RGB: ', rgbValues)
print('CMY: ', cmyValues) print('CMY: ', cmyValues)
@@ -99,11 +99,11 @@ def handleRGB(color) :
print('convert RGB: ', rgbValues, '\n') print('convert RGB: ', rgbValues, '\n')
hexCode = rgbToHex(rgbValues) hexCode = RGBtoHEX(rgbValues)
cmyValues = rgbToCMY(rgbValues) cmyValues = RGBtoCMY(rgbValues)
cmykValues = rgbToCMYK(rgbValues) cmykValues = RGBtoCMYK(rgbValues)
hslValues = rgbToHSVorHSL(rgbValues, True) hslValues = RGBtoHSVorHSL(rgbValues, 'hsl')
hsvValues = rgbToHSVorHSL(rgbValues, False) hsvValues = RGBtoHSVorHSL(rgbValues, 'hsv')
print('CMYK: ', cmykValues) print('CMYK: ', cmykValues)
print('Hex: ', hexCode) print('Hex: ', hexCode)
@@ -121,11 +121,11 @@ def handleCMY(color) :
print('convert CMY: ', cmyValues, '\n') print('convert CMY: ', cmyValues, '\n')
rgbValues = cmyToRGB(cmyValues) rgbValues = CMYtoRGB(cmyValues)
hexCode = rgbToHex(rgbValues) hexCode = RGBtoHEX(rgbValues)
cmykValues = rgbToCMYK(rgbValues) cmykValues = RGBtoCMYK(rgbValues)
hslValues = rgbToHSVorHSL(rgbValues, True) hslValues = RGBtoHSVorHSL(rgbValues, 'hsl')
hsvValues = rgbToHSVorHSL(rgbValues, False) hsvValues = RGBtoHSVorHSL(rgbValues, 'hsv')
print('Hex: ', hexCode) print('Hex: ', hexCode)
print('RGB: ', rgbValues) print('RGB: ', rgbValues)
@@ -143,11 +143,11 @@ def handleCMYK(color) :
print('convert CMYK: ', cmykValues, '\n') print('convert CMYK: ', cmykValues, '\n')
rgbValues = cmykToRGB(cmykValues) rgbValues = CMYKtoRGB(cmykValues)
hexCode = rgbToHex(rgbValues) hexCode = RGBtoHEX(rgbValues)
cmyValues = rgbToCMY(rgbValues) cmyValues = RGBtoCMY(rgbValues)
hslValues = rgbToHSVorHSL(rgbValues, True) hslValues = RGBtoHSVorHSL(rgbValues, 'hsl')
hsvValues = rgbToHSVorHSL(rgbValues, False) hsvValues = RGBtoHSVorHSL(rgbValues, 'hsv')
print('Hex: ', hexCode) print('Hex: ', hexCode)
print('RGB: ', rgbValues) print('RGB: ', rgbValues)
@@ -156,10 +156,10 @@ def handleCMYK(color) :
print('HSV: ', hsvValues) print('HSV: ', hsvValues)
# isHSL determines whether the function should handle HSL or HSV # isHSL determines whether the function should handle HSL or HSV
def handleHSVorHSL(color, isHSL) : def handleHSVorHSL(color, handle) :
# cleanse color code # cleanse color code
if len(color) == 1 : if len(color) == 1 :
if isHSL : if handle == 'hsl' :
color = color[0].lower().strip('hsl(').strip(')').replace('%', '').replace(' ', '').split(',') color = color[0].lower().strip('hsl(').strip(')').replace('%', '').replace(' ', '').split(',')
else : # is HSV else : # is HSV
color = color[0].lower().strip('hsv(').strip(')').replace('%', '').replace(' ', '').split(',') color = color[0].lower().strip('hsv(').strip(')').replace('%', '').replace(' ', '').split(',')
@@ -168,20 +168,20 @@ def handleHSVorHSL(color, isHSL) :
if validated is None : if validated is None :
return return
rgbValues = hslOrHSVToRGB(validated, isHSL) rgbValues = HSLorHSVToRGB(validated, handle)
hexCode = rgbToHex(rgbValues) hexCode = RGBtoHEX(rgbValues)
cmyValues = rgbToCMY(rgbValues) cmyValues = RGBtoCMY(rgbValues)
cmykValues = rgbToCMYK(rgbValues) cmykValues = RGBtoCMYK(rgbValues)
if isHSL : if handle == 'hsl' :
hsvValues = rgbToHSVorHSL(rgbValues, isHSL) hsvValues = RGBtoHSVorHSL(rgbValues, 'hsv')
else : # is HSV else : # is HSV
hslValues = rgbToHSVorHSL(rgbValues, isHSL) hslValues = RGBtoHSVorHSL(rgbValues, 'hsl')
print('Hex: ', hexCode) print('Hex: ', hexCode)
print('RGB: ', rgbValues) print('RGB: ', rgbValues)
print('CMY: ', cmyValues) print('CMY: ', cmyValues)
print('CMYK: ', cmykValues) print('CMYK: ', cmykValues)
if isHSL : if handle == 'hsl' :
print('HSV: ', hsvValues) print('HSV: ', hsvValues)
else : else :
print('HSL: ', hslValues) print('HSL: ', hslValues)
@@ -194,7 +194,7 @@ def handleHSVorHSL(color, isHSL) :
## ##
# Takes in a string, returns a list of 3 integers # Takes in a string, returns a list of 3 integers
def hexToRGB(hexCode) : def HEXtoRGB(hexCode) :
rgbValues = [] rgbValues = []
tempSum = 0 tempSum = 0
@@ -209,7 +209,7 @@ def hexToRGB(hexCode) :
return rgbValues return rgbValues
# Takes in a list of 3 integers, returns a string # Takes in a list of 3 integers, returns a string
def rgbToHex(rgbValues) : def RGBtoHEX(rgbValues) :
hexValue = '' hexValue = ''
for rgbValue in rgbValues : for rgbValue in rgbValues :
@@ -219,7 +219,7 @@ def rgbToHex(rgbValues) :
return hexValue return hexValue
# Takes in a list of 3 integers, returns a list of 3 floats (percentages) # Takes in a list of 3 integers, returns a list of 3 floats (percentages)
def rgbToCMY(rgbValues) : def RGBtoCMY(rgbValues) :
# Normalize RGB values # Normalize RGB values
normalRed = rgbValues[0] / 255 normalRed = rgbValues[0] / 255
normalGreen = rgbValues[1] / 255 normalGreen = rgbValues[1] / 255
@@ -233,7 +233,7 @@ def rgbToCMY(rgbValues) :
return [cyan * 100, magenta * 100, yellow * 100] return [cyan * 100, magenta * 100, yellow * 100]
# Takes in a list of 3 integers, returns a list of 4 floats (percentages) # Takes in a list of 3 integers, returns a list of 4 floats (percentages)
def rgbToCMYK(rgbValues) : def RGBtoCMYK(rgbValues) :
# Normalize RGB values # Normalize RGB values
normalRed = rgbValues[0] / 255 normalRed = rgbValues[0] / 255
normalGreen = rgbValues[1] / 255 normalGreen = rgbValues[1] / 255
@@ -253,7 +253,7 @@ def rgbToCMYK(rgbValues) :
return [cyan * 100, magenta * 100, yellow * 100, black * 100] return [cyan * 100, magenta * 100, yellow * 100, black * 100]
# Takes in a list of 3 integers, returns a list of 1 integer and 2 floats (percentages) # Takes in a list of 3 integers, returns a list of 1 integer and 2 floats (percentages)
def rgbToHSVorHSL(rgbValues, isHSL) : def RGBtoHSVorHSL(rgbValues, convertTo) :
hue = 0 hue = 0
saturation = 0.0 saturation = 0.0
value = 0.0 value = 0.0
@@ -269,7 +269,7 @@ def rgbToHSVorHSL(rgbValues, isHSL) :
xMin = min(normalRed, normalGreen, normalBlue) xMin = min(normalRed, normalGreen, normalBlue)
chroma = xMax - xMin chroma = xMax - xMin
# Convert # Convert by following formula
value = xMax value = xMax
lightness = (xMax + xMin) / 2 lightness = (xMax + xMin) / 2
@@ -277,27 +277,24 @@ def rgbToHSVorHSL(rgbValues, isHSL) :
if chroma == 0 : if chroma == 0 :
hue = 0 hue = 0
elif value == normalRed : elif value == normalRed :
hue = 60 * ((normalGreen - normalBlue) / chroma) % 6 hue = 60 * (((normalGreen - normalBlue) / chroma) % 6 )
elif value == normalGreen : elif value == normalGreen :
hue = 60 * (((normalBlue - normalRed) / chroma) + 2) hue = 60 * (((normalBlue - normalRed) / chroma) + 2)
elif value == value == normalBlue : elif value == normalBlue :
hue = 60 * (((normalRed - normalGreen) / chroma) + 4) hue = 60 * (((normalRed - normalGreen) / chroma) + 4)
if convertTo == 'hsl' :
if isHSL :
if (lightness != 0) and (lightness != 1) : if (lightness != 0) and (lightness != 1) :
saturation = (value - lightness) / min(lightness, 1 - lightness) saturation = (value - lightness) / min(lightness, 1 - lightness)
# else : keep zero
return [int(hue), saturation * 100, lightness * 100] return [int(hue), saturation * 100, lightness * 100]
else : else : # convert to HSV
if value != 0 : if value != 0 :
saturation = chroma / value saturation = chroma / value
# else : keep zero
return [int(hue), saturation * 100, value * 100] return [int(hue), saturation * 100, value * 100]
# Takes in a list of 3 floats, returns a list of 3 integers # Takes in a list of 3 floats, returns a list of 3 integers
def cmyToRGB(cmyValues) : def CMYtoRGB(cmyValues) :
red = (1 - (cmyValues[0] / 100)) * 255 red = (1 - (cmyValues[0] / 100)) * 255
green = (1 - (cmyValues[1] / 100)) * 255 green = (1 - (cmyValues[1] / 100)) * 255
blue = (1 - (cmyValues[2] / 100)) * 255 blue = (1 - (cmyValues[2] / 100)) * 255
@@ -305,7 +302,7 @@ def cmyToRGB(cmyValues) :
return [smartRound(red), smartRound(green), smartRound(blue)] return [smartRound(red), smartRound(green), smartRound(blue)]
# Takes in a list of 4 floats, returns a list of 3 integers # Takes in a list of 4 floats, returns a list of 3 integers
def cmykToRGB(cmykValues) : def CMYKtoRGB(cmykValues) :
x = 1 - (cmykValues[3] / 100) x = 1 - (cmykValues[3] / 100)
red = 255 * (1 - (cmykValues[0] / 100)) * x red = 255 * (1 - (cmykValues[0] / 100)) * x
green = 255 * (1 - (cmykValues[1] / 100)) * x green = 255 * (1 - (cmykValues[1] / 100)) * x
@@ -314,59 +311,58 @@ def cmykToRGB(cmykValues) :
return [smartRound(red), smartRound(green), smartRound(blue)] return [smartRound(red), smartRound(green), smartRound(blue)]
# Takes in a list with 1 integer and 2 floats (in that order), and returns 3 integers # Takes in a list with 1 integer and 2 floats (in that order), and returns 3 integers
def hslOrHSVToRGB(values, isHSL) : def HSLorHSVToRGB(values, convertFrom) :
print('hslToRGB: ', values) print('hslToRGB: ', values)
normalSaturation = values[1] / 100 normalSaturation = values[1] / 100
normalLorV= values[2] / 100 normalLorV= values[2] / 100
# Perform first part of conversion # Perform first part of conversion
if isHSL : if convertFrom == 'hsl' :
chroma = (1 - math.fabs((2 * normalLorV) - 1)) * normalSaturation chroma = (1 - math.fabs((2 * normalLorV) - 1)) * normalSaturation
m = normalLorV - (chroma / 2)
else : # is HSV else : # is HSV
chroma = normalLorV * normalSaturation chroma = normalLorV * normalSaturation
m = normalLorV - chroma
hPrime = int(values[0] / 60) hPrime = values[0] / 60
x = chroma * (1 - math.fabs(hPrime % 2 - 1)) temp = (hPrime % 2) - 1
m = normalLorV - (chroma / 2) x = chroma * (1 - math.fabs(temp))
if (hPrime >= 0) and (hPrime < 1) : if (hPrime >= 0) and (hPrime < 1) :
R = chroma + m R = chroma + m
G = x + m G = x + m
B = 0 + m B = 0 + m
return [R * 255, G * 255, B * 255] return [smartRound(R * 255), smartRound(G * 255), smartRound(B * 255)]
elif (hPrime >= 1) and (hPrime < 2) : elif (hPrime >= 1) and (hPrime < 2) :
R = x + m R = x + m
G = chroma + m G = chroma + m
B = 0 + m B = 0 + m
return [R * 255, G * 255, B * 255] return [smartRound(R * 255), smartRound(G * 255), smartRound(B * 255)]
elif (hPrime >= 2) and (hPrime < 3) : elif (hPrime >= 2) and (hPrime < 3) :
R = 0 + m R = 0 + m
G = chroma + m G = chroma + m
B = x + m B = x + m
return [R * 255, G * 255, B * 255] return [smartRound(R * 255), smartRound(G * 255), smartRound(B * 255)]
elif (hPrime >= 3* 255) and (hPrime < 4) : elif (hPrime >= 3) and (hPrime < 4) :
R = 0 + m R = 0 + m
G = x + m G = x + m
B = chroma + m B = chroma + m
return [R * 255, G * 255, B * 255] return [smartRound(R * 255), smartRound(G * 255), smartRound(B * 255)]
elif (hPrime >= 4) and (hPrime < 5) : elif (hPrime >= 4) and (hPrime < 5) :
R = x + m R = x + m
G = 0 + m G = 0 + m
B = chroma + m B = chroma + m
return [R * 255, G * 255, B * 255] return [smartRound(R * 255), smartRound(G * 255), smartRound(B * 255)]
elif (hPrime >= 5) and (hPrime <= 6) : elif (hPrime >= 5) and (hPrime <= 6) :
R = chrome + m R = chrome + m
G = 0 + m G = 0 + m
B = x + m B = x + m
return [R * 255, G * 255, B * 255] return [smartRound(R * 255), smartRound(G * 255), smartRound(B * 255)]
else : else :
print('RGB to HSV/HSL conversion failed') print('RGB to HSV/HSL conversion failed')
return return
def hsvToRGB(rgbValues) :
print('hsvToRGB')
## ##
# VALIDATION SECTION # VALIDATION SECTION
## ##