refactoring / code organization

This commit is contained in:
2024-11-30 23:25:14 -05:00
parent 4570a7d798
commit 2982d65162
+75 -45
View File
@@ -22,8 +22,7 @@ def main():
# debug print # debug print
print('args type: ', type(vars(args))) print(args)
print(args, '\n')
# INPUT SYNTAX VALIDATION # INPUT SYNTAX VALIDATION
@@ -42,34 +41,23 @@ def main():
# HANDLE RGB INPUT # HANDLE RGB INPUT
if args.rgb : if args.rgb :
# cleanse any non-numerical stuff
if len(color) == 1 :
color = color[0].lower().strip('rgb(').strip(')').replace(' ', '').split(',')
if not validateRGB(color) :
return
for i in range(len(color)) :
color[i] = int(color[i])
convertFromRGB(color) convertFromRGB(color)
# HANDLE CMYK INPUT # HANDLE CMYK INPUT
if args.cmyk : if args.cmyk :
# cleanse any non-numerical stuff
if len(color) == 1 :
color = color[0].lower().strip('cmyk(').strip(')').replace('%', '').replace(' ', '').split(',')
if not validateCMYK(color) :
return
print('convert cmyk: ', color)
convertFromCMYK(color) convertFromCMYK(color)
## ##
# HEX CONVERSION SECTION # HEX CONVERSION SECTION
## ##
# Takes in valid RGB code and converts it to the other formats # Takes in valid RGB code and converts it to the other formats
def convertFromHex(hexCode) : def convertFromHex(hexCode) :
print('convert hex: ', hexCode) print('convert hex: ', hexCode, '\n')
rgbValues = hexToRGB(hexCode) rgbValues = hexToRGB(hexCode)
print("RGB: ", rgbValues) print("RGB: ", rgbValues)
@@ -89,13 +77,25 @@ def rgbToHex(rgbValues) :
return hexValue return hexValue
##
##
# RGB CONVERSION SECTION # RGB CONVERSION SECTION
## ##
# Takes in valid RGB code and converts it to the other formats # Takes in valid RGB code and converts it to the other formats
def convertFromRGB(rgbValues) : def convertFromRGB(color) :
print('convert RGB: ', rgbValues) # cleanse any non-numerical stuff
if len(color) == 1 :
color = color[0].lower().strip('rgb(').strip(')').replace(' ', '').split(',')
rgbValues = validateRGB(color)
if rgbValues is None :
return
for i in range(len(rgbValues)) :
rgbValues[i] = int(rgbValues[i])
print('convert RGB: ', rgbValues, '\n')
hexCode = rgbToHex(rgbValues) hexCode = rgbToHex(rgbValues)
print('Hex: ', hexCode) print('Hex: ', hexCode)
@@ -119,12 +119,37 @@ def hexToRGB(hexCode) :
return rgbValues return rgbValues
def cmykToRGB(cmykValues) :
x = 1 - (cmykValues[3] / 100)
red = 255 * (1 - (cmykValues[0] / 100)) * x
green = 255 * (1 - (cmykValues[1] / 100)) * x
blue = 255 * (1 - (cmykValues[2] / 100)) * x
return [int(red), int(green), int(blue)]
## ##
# CMYK CONVERSION SECTION # CMYK CONVERSION SECTION
## ##
def convertFromCMYK() : def convertFromCMYK(color) :
print('huh') # cleanse any non-numerical stuff
if len(color) == 1 :
color = color[0].lower().strip('cmyk(').strip(')').replace('%', '').replace(' ', '').split(',')
cmykValues = validateCMYK(color)
if cmykValues is None :
return
print('convert CMYK: ', cmykValues, '\n')
rgbValues = cmykToRGB(cmykValues)
print('RGB: ', rgbValues)
hexCode = rgbToHex(rgbValues)
print('Hex: ', hexCode)
def rgbToCMYK(rgbValues) : def rgbToCMYK(rgbValues) :
# Normalize RGB values # Normalize RGB values
@@ -141,12 +166,10 @@ def rgbToCMYK(rgbValues) :
magenta = (1 - normalGreen - black) / x magenta = (1 - normalGreen - black) / x
yellow = (1 - normalBlue - black) / x yellow = (1 - normalBlue - black) / x
return [cyan, magenta, yellow, black] return [round(cyan * 100, 2), round(magenta * 100, 2), round(yellow * 100, 2), round(black * 100, 2)]
def cmykToRGB(cmykValues) :
print('ddf')
## ##
# INPUT VALIDATION SECTION # INPUT VALIDATION SECTION
## ##
@@ -168,45 +191,45 @@ def validateHex(value) :
return True return True
# Takes in a list of strings. Returns same list as integers if valid RGB values.
# Takes in a list of numerical strings. Returns True if valid RGB values.
def validateRGB(values) : def validateRGB(values) :
intValues = []
if len(values) != 3 : if len(values) != 3 :
print('ERROR: Improper number of values for RGB (should be 3)') print('ERROR: Improper number of values for RGB (should be 3)')
return False return
for value in values : for value in values :
if not value.strip().isnumeric() : if not value.strip().isnumeric() :
print('ERROR: Improper format for RGB value(s)') print('ERROR: Improper format for RGB value(s)')
return False return
value = int(value) value = int(value)
intValues.append(value)
if (value < 0) or (value > 255) : if (value < 0) or (value > 255) :
print('ERROR: Each RBG value must be between 0-255') print('ERROR: Each RBG value must be between 0-255')
return False return
return True
return intValues
# Takes in a string. Returns True if valid CMYK values. # Takes in a list of strings. Returns same list as integers if valid CMYK values.
def validateCMYK(values) : def validateCMYK(values) :
intValues = []
if len(values) != 4 : if len(values) != 4 :
print('ERROR: Improper number of values for CMYK (should be 4)') print('ERROR: Improper number of values for CMYK (should be 4)')
return False return
for value in values : for value in values :
if not value.isnumeric() : if not value.isnumeric() :
print('ERROR: Improper format for CMYK value(s). All values must be numeric and between 0-100(%)!') print('ERROR: Improper format for CMYK value(s). All values must be numeric and between 0-100(%)!')
return False return
value = int(value) value = int(value)
intValues.append(value)
if (value < 0) or (value > 100) : if (value < 0) or (value > 100) :
print('ERROR: Each CMYK value must be between 0-100(%)') print('ERROR: Each CMYK value must be between 0-100(%)')
return False return
return True return intValues
##
# GENERAL UTILITIES
##
# Takes in the program's arguments generated by argparse. Returns True if valid arguments # Takes in the program's arguments generated by argparse. Returns True if valid arguments
def validateArguments(args) : def validateArguments(args) :
@@ -226,7 +249,14 @@ def validateArguments(args) :
return False return False
return True return True
##
# GENERAL UTILITIES
##
# Takes in a decimal number and converts it to hexadecimal # Takes in a decimal number and converts it to hexadecimal
def hex(number) : def hex(number) :