From da81ef7e15ac5229f14e1cf45470061941afc8ac Mon Sep 17 00:00:00 2001 From: Simon Date: Mon, 20 Jan 2025 18:34:52 -0500 Subject: [PATCH] fixed hexcode validation edge-case, README update --- README.md | 55 ++++++++++++++++++++-------------------------- color-converter.py | 17 +++++++------- 2 files changed, 33 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index aefbc39..11fb0d5 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,16 @@ # color-converter -A CLI color code conversion utility written in Python. +A robust CLI color conversion utility written in Python. -# Project Background +# Features -I started this project because I found myself converting between Hex and RGB a lot while ricing. As I was frequently visiting various color picker websites, I began thinking more and more about how color conversions even work in the first place. - -Using any random color-picker online to go from RBG->Hex (and vice versa) works plenty fine, but I think CLI tools are cool, and I couldn't find any CLI color conversion utilities. +- Supports conversions between hexcode, RGB, CMY, CMYK, HSL, and HSV +- Lenient automatic input detection +- Bulk conversions to and from files # Usage -This program supports hexcodes, RGB, CMY, CMYK, HSL, and HSV. - -Simply supply a color in any of the supported formats as a string, and you will receive the equivalent color codes in all of the supported formats. - -For example, a simple input with all conversions: +Simply supply one or more colors in any of the supported formats as a string, and it will perform all supported conversions. ``` $ python color-converter.py "#85feab" @@ -26,22 +22,23 @@ hsl(138.84, 98.37, 75.88) hsv(138.84, 47.64, 99.61) ``` -You can use flags to limit the output: +You can also use flags to limit the output: ``` -$ python color-converter.py -cmy -hsv "#85feab" -cmy(47.84, 0.39, 32.94) -hsv(138.84, 47.64, 99.61) +$ python color-converter.py -rgb "#abc123" "#def456" +rgb(171, 193, 35) + +rgb(222, 244, 86) + ``` --- It can also function as a color translation utility for converting colors in bulk. -It can handle more than one input on the command line, or you can input files. You can limit the output to one or more specific formats. -For example, you could take a file that has color codes on each line, each in different formats, and have them all converted to a single format. +For example, you could take a file that has color codes in various formats and have them all converted to a single format. -For example, use this command: +To do what was just described, use this command: ``` $ python color-converter -cmyk -i "input_file" -o "output_file" @@ -55,7 +52,7 @@ rgb(1, 2, 3) HSV(1, 2, 3) ``` -and the output file is populated with: +and the output file gets populated with: ``` cmyk(66.67%, 33.33%, 0%, 98.82%) @@ -73,7 +70,7 @@ For example, these are all valid strings to input 100, 200, and 300 as RGB value ``` "rgb(100, 200, 300)" -" rgb: 100 200 300" +" RGB: 100 200 300" "rGb-100 . 200^300" ``` @@ -84,22 +81,18 @@ While the program will always try to auto-detect the input format, If you only h For example: ``` -$ python color-converter.py -rgb -isHex "123abc" -rgb(18, 58, 188) - -// or - -$ python color-converter.py -hsl -isCmy "12 32 90" -#e0ad19 -rgb(224, 173, 25) -cmy(12.00, 32.00, 90.00) -cmyk(0.00, 22.77, 88.84, 12.16) -hsl(44.62, 79.92, 48.82) -hsv(44.62, 88.84, 87.84) +$ python color-converter.py -rgb -isCmyk "1 2 3 4" +rgb(242, 240, 237) ``` (Don't forget to use the `-h` or `--help` flags for in-terminal help) +# Project Background + +I started this project because I found myself converting between Hex and RGB a lot while ricing. As I was frequently visiting various color picker websites, I began thinking more and more about how color conversions even work in the first place. + +Using any random color-picker online to go from RBG->Hex (and vice versa) works plenty fine, but I think CLI tools are cool, and I couldn't find any CLI color conversion utilities. + # Conversion Sources Hex<->RGB: diff --git a/color-converter.py b/color-converter.py index a0123e0..6ed4194 100644 --- a/color-converter.py +++ b/color-converter.py @@ -6,7 +6,7 @@ TYPES = ['hex', 'rgb', 'cmy', 'cmyk', 'hsl', 'hsv'] HEX_LETTERS = ['a', 'b', 'c', 'd', 'e', 'f'] -OUTPUT = 'stdout' +OUTPUT_DESTINATION = 'stdout' VERBOSE = False @@ -54,10 +54,10 @@ def main(): # determine if output should be (over?)written to file if args.output : - global OUTPUT - OUTPUT = args.output + global OUTPUT_DESTINATION + OUTPUT_DESTINATION = args.output if not args.append : - output_file = open(OUTPUT, 'w') + output_file = open(OUTPUT_DESTINATION, 'w') output_file.write('') output_file.close() @@ -446,13 +446,13 @@ def HSLorHSVToRGB(values, convertFrom) : # 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] + hexcode = extractValues(value, 1, True) if not hexcode : print('ERROR: Improper format for hex code (see --help)') return - return hexcode + return hexcode[0] # Takes in a string. Returns same list as integers if valid RGB values. def validateRGB(color) : @@ -460,6 +460,7 @@ def validateRGB(color) : rgbValues = extractValues(color, 3) if not rgbValues : + print('bo') return intValues = [] @@ -633,7 +634,7 @@ def printConversions(convertedValues) : output.append(colorCode) # deliver formatted output - if OUTPUT != 'stdout' : + if OUTPUT_DESTINATION != 'stdout' : ''' TODO FOR EFFICIENCY: @@ -644,7 +645,7 @@ def printConversions(convertedValues) : to actually open the file back in main() at the point when we evaluate args, and then close it after everything in main() has ran... food for thought. ''' - with open(OUTPUT, 'a', encoding='utf-8') as file : + with open(OUTPUT_DESTINATION, 'a', encoding='utf-8') as file : for color in output : file.write(color + '\n') file.write('\n')