1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
|
import math
import argparse
# Order of types must match order of arguments defined
# (or else arg validation will no longer work properly)
TYPES = ['hex', 'rgb', 'cmy', 'cmyk', 'hsl', 'hsv']
# look into LAB
HEX_LETTERS = ['a', 'b', 'c', 'd', 'e', 'f']
OUTPUT = 'stdout'
APPEND = False
def main():
# Set up arguments
parser = argparse.ArgumentParser()
parser.add_argument('-hex', action='store_true', help='output Hex')
parser.add_argument('-rgb', action='store_true', help='output RGB')
parser.add_argument('-cmy', action='store_true', help='output CMY')
parser.add_argument('-cmyk', action='store_true', help='output CMYK')
parser.add_argument('-hsl', action='store_true', help='output HSL')
parser.add_argument('-hsv', action='store_true', help='output HSV')
parser.add_argument('--input', '-i', help='name of the input file containing color codes to process')
parser.add_argument('--output', '-o', help='the name of the file to store output in (will create file if doesn\'t exist, will OVERWRITE existing file\'s contents)')
parser.add_argument('--append', '-a', help='if an output file is specified, the conversions will be appended to the file')
'''
parser.add_argument('-isHex', action='store_true', help='convert from hex (accepts hexadecimal input [ffffff] or string ["#ffffff"] (case insensitive, "#" is optional in string)')
parser.add_argument('-isRgb', action='store_true', help='convert from RGB (accepts integer input [R G B], or string [\"rgb(R, G, B)\"] (case/whitespace insensitive)')
parser.add_argument('-isCmy', action='store_true', help='convert from CMY (accepts integer input [C M Y], or string [\"cmy(C, M, Y)\"] (case/whitespace insensitive)')
parser.add_argument('-isCmyk', action='store_true', help='convert from CMYK (accepts integer input [C M Y K], or string [\"cmyk(C, M, Y, K)\"] (case/whitespace insensitive)')
parser.add_argument('-isHsl', action='store_true', help='convert from HSL (accepts integer input [H S L], or string [\"hsl(H, S, L)\"] (case/whitespace insensitive)')
parser.add_argument('-isHsv', action='store_true', help='convert from HSV (accepts integer input [H S V], or string [\"hsv(H, S, V)\"] (case/whitespace insensitive)')
'''
parser.add_argument('color', nargs='*', help='accepts a color in Hex, RGB, CMY, CMYK, or HSL and performs format conversions (does not support CMYK profiles, conversions are uncalibrated)')
args = parser.parse_args()
# debug print
print(args)
print('#########')
print(args.color)
''' ARGS PROCESSING, determine which conversions to perform '''
outputFormats = []
flagsActive = 0
for flag in vars(args) :
if flag in TYPES :
flagsActive += 1
outputFormats.append(flag)
# if no conversion flags specified, perform every format conversion
if flagsActive == 0 :
for formats in TYPES :
outputFormats.append(format)
# if file output is specified, set global vars for reference later
if args.output :
global OUTPUT
OUTPUT = args.output
if args.append :
global APPEND
APPEND = True
''' GET COLORS '''
colorCodes = []
# if provided a file of values
if args.input :
with open(args.input, 'r', encoding='utf-8') as file :
for line in file :
colorCodes.append(file.strip())
else :
colors = args.color
''' PROCESS COLORS '''
for color in colors :
# Try to automatically handle value
if detectColorFormat(color, outputFormats) :
continue
# if we can't auto-detect the format, check args for guidance
elif args.isHex :
handleHex(color, outputFormats)
elif args.isRgb :
handleRGB(color, outputFormats)
elif args.isCmy :
handleCMY(color, outputFormats)
elif args.isCmyk :
handleCMYK(color, outputFormats)
elif args.isHsl :
handleHSVorHSL(color, 'hsl', outputFormats)
elif args.isHsv :
handleHSVorHSL(color, 'hsv', outputFormats)
##
# COLOR HANDLERS
##
# takes in a map of color values and either prints their values to STDOUT or into a specified file
def printConversions(convertedValues) :
# format converted values into better output strings
output = []
for colorFormat in TYPES :
if convertedValues.get(colorFormat, None) is None :
continue
elif colorFormat == 'hex' :
hexCode = '#' + convertedValues['hex']
output.append(hexCode)
else :
# format strings for xyz(a,b,c) type formats
colorCode = colorFormat + '('
numValues = len(convertedValues[colorFormat])
for valueIndex in range(numValues) :
value = convertedValues[colorFormat][valueIndex]
if type(value) == float :
value = f"{value:.2f}"
if valueIndex == numValues - 1 :
colorCode += str(value) + ')'
else :
colorCode += str(value) + ', '
output.append(colorCode)
# determine how to deliver output
if OUTPUT != 'stdout' :
with open(OUTPUT, 'w', encoding='utf-8') as file :
for color in output :
file.write(color + '\n')
else :
for color in output :
print(color)
# Takes in valid RGB code and converts it to the other formats
def handleHex(color, outputFormats = TYPES) :
hexCode = validateHex(color)
if hexCode is None :
return
print('convert hex: ', hexCode, '\n')
rgbValues = HEXtoRGB(hexCode)
outputs = {}
if 'rgb' in outputFormats :
outputs['rgb'] = rgbValues
if 'cmy' in outputFormats :
outputs['cmy'] = RGBtoCMY(rgbValues)
if 'cmyk' in outputFormats :
outputs['cmyk'] = RGBtoCMYK(rgbValues)
if 'hsl' in outputFormats :
outputs['hsl'] = RGBtoHSVorHSL(rgbValues, 'hsl')
if 'hsv' in outputFormats :
outputs['hsv'] = RGBtoHSVorHSL(rgbValues, 'hsv')
printConversions(outputs)
# Takes in valid RGB code and converts it to the other formats
def handleRGB(color, outputFormats = TYPES) :
rgbValues = validateRGB(color)
if rgbValues is None :
return
print('convert RGB: ', rgbValues, '\n')
hexCode = RGBtoHEX(rgbValues)
cmyValues = RGBtoCMY(rgbValues)
cmykValues = RGBtoCMYK(rgbValues)
hslValues = RGBtoHSVorHSL(rgbValues, 'hsl')
hsvValues = RGBtoHSVorHSL(rgbValues, 'hsv')
print('CMYK: ', cmykValues)
print('Hex: ', hexCode)
print('CMY: ', cmyValues)
print('HSL: ', hslValues)
print('HSV: ', hsvValues)
def handleCMY(color, outputFormats = TYPES) :
cmyValues = validateCMYorCMYK(color, False)
if cmyValues is None :
return
print('convert CMY: ', cmyValues, '\n')
rgbValues = CMYtoRGB(cmyValues)
hexCode = RGBtoHEX(rgbValues)
cmykValues = RGBtoCMYK(rgbValues)
hslValues = RGBtoHSVorHSL(rgbValues, 'hsl')
hsvValues = RGBtoHSVorHSL(rgbValues, 'hsv')
print('Hex: ', hexCode)
print('RGB: ', rgbValues)
print('CMYK: ', cmykValues)
print('HSL: ', hslValues)
print('HSV: ', hsvValues)
def handleCMYK(color, outputFormats = TYPES) :
cmykValues = validateCMYorCMYK(color, True)
if cmykValues is None :
return
print('convert CMYK: ', cmykValues, '\n')
rgbValues = CMYKtoRGB(cmykValues)
hexCode = RGBtoHEX(rgbValues)
cmyValues = RGBtoCMY(rgbValues)
hslValues = RGBtoHSVorHSL(rgbValues, 'hsl')
hsvValues = RGBtoHSVorHSL(rgbValues, 'hsv')
print('Hex: ', hexCode)
print('RGB: ', rgbValues)
print('CMY: ', cmyValues)
print('HSL: ', hslValues)
print('HSV: ', hsvValues)
# isHSL determines whether the function should handle HSL or HSV
def handleHSVorHSL(color, handle, outputFormats = TYPES) :
validated = validateHSLorHSV(color)
if validated is None :
return
rgbValues = HSLorHSVToRGB(validated, handle)
hexCode = RGBtoHEX(rgbValues)
cmyValues = RGBtoCMY(rgbValues)
cmykValues = RGBtoCMYK(rgbValues)
if handle == 'hsl' :
hsvValues = RGBtoHSVorHSL(rgbValues, 'hsv')
else : # is HSV
hslValues = RGBtoHSVorHSL(rgbValues, 'hsl')
print('Hex: ', hexCode)
print('RGB: ', rgbValues)
print('CMY: ', cmyValues)
print('CMYK: ', cmykValues)
if handle == 'hsl' :
print('HSV: ', hsvValues)
else :
print('HSL: ', hslValues)
##
# CONVERSION FUNCTIONS
##
# Takes in a string, returns a list of 3 integers
def HEXtoRGB(hexCode) :
rgbValues = []
tempSum = 0
i = 0
while i < 6 :
tempSum += int(hexCode[i], 16) * 16
tempSum += int(hexCode[i+1], 16)
rgbValues.append(tempSum)
i = i + 2
tempSum = 0
return rgbValues
# Takes in a list of 3 integers, returns a string
def RGBtoHEX(rgbValues) :
hexValue = ''
for rgbValue in rgbValues :
hexValue += hex(rgbValue / 16)
hexValue += hex(rgbValue % 16)
return hexValue
# Takes in a list of 3 integers, returns a list of 3 floats (percentages)
def RGBtoCMY(rgbValues) :
# Normalize RGB values
normalRed = rgbValues[0] / 255
normalGreen = rgbValues[1] / 255
normalBlue = rgbValues[2] / 255
# Convert
cyan = 1 - normalRed
magenta = 1 - normalGreen
yellow = 1 - normalBlue
return [cyan * 100, magenta * 100, yellow * 100]
# Takes in a list of 3 integers, returns a list of 4 floats (percentages)
def RGBtoCMYK(rgbValues) :
# Normalize RGB values
normalRed = rgbValues[0] / 255
normalGreen = rgbValues[1] / 255
normalBlue = rgbValues[2] / 255
black = 1 - max(normalRed, normalGreen, normalBlue)
x = 1 - black
# Convert
if x == 0 :
return [0, 0, 0, round(black * 100, 2)]
else :
cyan = (1 - normalRed - black) / x
magenta = (1 - normalGreen - black) / x
yellow = (1 - normalBlue - black) / x
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)
def RGBtoHSVorHSL(rgbValues, convertTo) :
hue = 0
saturation = 0.0
value = 0.0
lightness = 0.0
# Normalize RGB values
normalRed = rgbValues[0] / 255
normalGreen = rgbValues[1] / 255
normalBlue = rgbValues[2] / 255
# Establish variables for formula
xMax = max(normalRed, normalGreen, normalBlue)
xMin = min(normalRed, normalGreen, normalBlue)
chroma = xMax - xMin
# Convert by following formula
value = xMax
lightness = (xMax + xMin) / 2
if chroma == 0 :
hue = 0
elif value == normalRed :
hue = 60 * (((normalGreen - normalBlue) / chroma) % 6 )
elif value == normalGreen :
hue = 60 * (((normalBlue - normalRed) / chroma) + 2)
elif value == normalBlue :
hue = 60 * (((normalRed - normalGreen) / chroma) + 4)
if convertTo == 'hsl' :
if (lightness != 0) and (lightness != 1) :
saturation = (value - lightness) / min(lightness, 1 - lightness)
return [int(hue), saturation * 100, lightness * 100]
else : # convert to HSV
if value != 0 :
saturation = chroma / value
return [int(hue), saturation * 100, value * 100]
# Takes in a list of 3 floats, returns a list of 3 integers
def CMYtoRGB(cmyValues) :
red = (1 - (cmyValues[0] / 100)) * 255
green = (1 - (cmyValues[1] / 100)) * 255
blue = (1 - (cmyValues[2] / 100)) * 255
return [smartRound(red), smartRound(green), smartRound(blue)]
# Takes in a list of 4 floats, returns a list of 3 integers
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 [smartRound(red), smartRound(green), smartRound(blue)]
# Takes in a list with 1 integer and 2 floats (in that order), and returns 3 integers
def HSLorHSVToRGB(values, convertFrom) :
normalSaturation = values[1] / 100
normalLorV= values[2] / 100
# Perform first part of conversion
if convertFrom == 'hsl' :
chroma = (1 - math.fabs((2 * normalLorV) - 1)) * normalSaturation
m = normalLorV - (chroma / 2)
else : # is HSV
chroma = normalLorV * normalSaturation
m = normalLorV - chroma
hPrime = values[0] / 60
temp = (hPrime % 2) - 1
x = chroma * (1 - math.fabs(temp))
if (hPrime >= 0) and (hPrime < 1) :
R = chroma + m
G = x + m
B = 0 + m
return [smartRound(R * 255), smartRound(G * 255), smartRound(B * 255)]
elif (hPrime >= 1) and (hPrime < 2) :
R = x + m
G = chroma + m
B = 0 + m
return [smartRound(R * 255), smartRound(G * 255), smartRound(B * 255)]
elif (hPrime >= 2) and (hPrime < 3) :
R = 0 + m
G = chroma + m
B = x + m
return [smartRound(R * 255), smartRound(G * 255), smartRound(B * 255)]
elif (hPrime >= 3) and (hPrime < 4) :
R = 0 + m
G = x + m
B = chroma + m
return [smartRound(R * 255), smartRound(G * 255), smartRound(B * 255)]
elif (hPrime >= 4) and (hPrime < 5) :
R = x + m
G = 0 + m
B = chroma + m
return [smartRound(R * 255), smartRound(G * 255), smartRound(B * 255)]
elif (hPrime >= 5) and (hPrime <= 6) :
R = chrome + m
G = 0 + m
B = x + m
return [smartRound(R * 255), smartRound(G * 255), smartRound(B * 255)]
else :
print('RGB to HSV/HSL conversion failed')
return
##
# INPUT VALIDATION
##
def detectColorFormat(color, outputFormats) :
if '#' in color :
handleHex(color, outputFormats)
return True
elif 'rgb' in color :
handleRGB(color, outputFormats)
return True
elif 'cmyk' in color :
handleCMYK(color, outputFormats)
return True
elif 'cmy' in color :
handleCMY(color, outputFormats)
return True
elif color.strip().startswith('hsl') :
handleHSVorHSL(color, 'hsl', outputFormats)
return True
elif color.strip().startswith('hsv') :
handleHSVorHSL(color, 'hsv', outputFormats)
return True
else :
return False
# Extracts numerical values from a string.
# Parameters:
# color: string containing value(s)
# numValues: determines how many values to extract (e.g. 3 for RGB, 4 for CMYK)
# isHex: determines if we're looking for hex rather than decimal
# returns: list of extracted color/number values in string form
def extractValues(color, numValues, isHex = False) :
print(color)
i = 0
tempValue = ''
extractedValues = []
if isHex :
# search for hex values
while i < len(color) :
if (color[i].isnumeric()) or (color[i] in HEX_LETTERS) :
tempValue += color[i]
else :
tempValue = ''
if len(tempValue) == 6 :
extractedValues.append(tempValue)
tempValue = ''
if len(extractedValues) == numValues :
break
i = i + 1
if (len(extractedValues) != numValues) and (len(tempValue) == 6) :
extractedValues.append(tempValue)
else :
# search for decimal values
while i < len(color) :
if color[i].isnumeric() or color[i] == '.' :
tempValue += color[i]
elif len(tempValue) > 0 :
extractedValues.append(tempValue)
tempValue = ''
if len(extractedValues) == numValues :
break
i = i + 1
if (len(extractedValues) != numValues) and (len(tempValue) > 0) :
extractedValues.append(tempValue)
if len(extractedValues) != numValues :
print(f'Could not extract the correct number of values from input: {color}. numValues requested: {numValues}, isHex: {isHex}, Values successfully extracted: {len(extractedValues)}, {extractedValues}')
return False
return extractedValues
# Takes in a string.
# If string contains a valid Hex color code, it gets returned as a string.
def validateHex(value) :
# attempt to extract hex code
hexcode = extractValues(value, 1, True)[0]
if hexcode is None :
print('ERROR: Improper format for hex code (see --help)')
return
return hexcode
# Takes in a string. Returns same list as integers if valid RGB values.
def validateRGB(color) :
# extract 3 numbers from the provided values
rgbValues = extractValues(color, 3)
if rgbValues is None:
return
intValues = []
for value in rgbValues :
# TODO see if i should smartround here instead of int cast
value = int(value)
if (value < 0) or (value > 255) :
print('ERROR: Each RBG value must be between 0-255')
return
intValues.append(value)
return intValues
# Takes in a list of 3 or 4 strings. Returns same list as integers if valid CMYK values.
def validateCMYorCMYK(color, include_K) :
if include_K :
values = extractValues(color, 4)
else :
values = extractValues(color, 3)
if values is None :
return
floatValues = []
for value in values :
value = float(value)
if (value < 0) or (value > 100) :
print('ERROR: Each CMY(K) value must be between 0,0-100.0(%)')
return
floatValues.append(value)
return floatValues
# Takes in a list of 3 strings. Returns same list as 1 integer and 2 floats
def validateHSLorHSV(color) :
color = extractValues(color, 3)
if color is None :
return
for i in range(3) :
if i == 0 :
color[i] = smartRound(color[i])
else :
color[i] = float(color[i])
if (color[0] < 0) or (color[0] > 255) :
print('ERROR: Invalid hue value (should be 0-255)')
return
if (color[1] < 0) or (color[1] > 100) :
print('ERROR: Invalid saturation value (should be 0.0-100.0)')
return
if (color[2] < 0) or (color[2] > 100) :
print('ERROR: Invalid lightness/value value (should be 0.0-100.0)')
return
return [color[0], color[1], color[2]]
# Takes in the program's arguments generated by argparse. Returns True if valid arguments
def validateArguments(args) :
# check that only one input flag is being used
'''flagsActive = 0
for flag in range(len(vars(args))-1) :
# this uses our TYPES list to key into the args dictionary and determine how many flags are True
if vars(args)[TYPES[flag]] :
flagsActive += 1
if flagsActive > 1 :
print('ERROR: Currently this tool only supports one input type at a time. Too many flags!')
return False'''
# if (flagsActive == 0) or (len(args.color) == 0) :
if (len(args.color) == 0) :
print('ERROR: Must enter both an input flag and color code (did you forget to wrap color code in quotes?)\nFor more info, use the \'-h\' or \'--help\' flag.')
return False
return True
##
# GENERAL UTILITIES
##
# Takes in a float, returns the nearest integer
def smartRound(value) :
value = float(value)
if (value % 1) > .50 :
return math.ceil(value)
else :
return math.floor(value)
# Takes in a decimal number and converts it to hexadecimal
def hex(number) :
number = int(number)
if number > 16 :
print("ERROR: Decimal to Hexidecimal conversion failed")
return "ERROR: Decimal to Hexidecimal conversion failed"
if number < 10 :
return str(number)
return HEX_LETTERS[number % 10]
# init
if __name__ == '__main__' :
main()
|