fixed hexcode validation edge-case, README update

This commit is contained in:
2025-01-20 18:34:52 -05:00
parent 0d0f27b944
commit da81ef7e15
2 changed files with 33 additions and 39 deletions
+24 -31
View File
@@ -1,20 +1,16 @@
# color-converter # 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. - Supports conversions between hexcode, RGB, CMY, CMYK, HSL, and HSV
- Lenient automatic input detection
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. - Bulk conversions to and from files
# Usage # Usage
This program supports hexcodes, RGB, CMY, CMYK, HSL, and HSV. Simply supply one or more colors in any of the supported formats as a string, and it will perform all supported conversions.
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:
``` ```
$ python color-converter.py "#85feab" $ python color-converter.py "#85feab"
@@ -26,22 +22,23 @@ hsl(138.84, 98.37, 75.88)
hsv(138.84, 47.64, 99.61) 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" $ python color-converter.py -rgb "#abc123" "#def456"
cmy(47.84, 0.39, 32.94) rgb(171, 193, 35)
hsv(138.84, 47.64, 99.61)
rgb(222, 244, 86)
``` ```
--- ---
It can also function as a color translation utility for converting colors in bulk. 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 in various formats and have them all converted to a single format.
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, use this command: To do what was just described, use this command:
``` ```
$ python color-converter -cmyk -i "input_file" -o "output_file" $ python color-converter -cmyk -i "input_file" -o "output_file"
@@ -55,7 +52,7 @@ rgb(1, 2, 3)
HSV(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%) 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"
"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: For example:
``` ```
$ python color-converter.py -rgb -isHex "123abc" $ python color-converter.py -rgb -isCmyk "1 2 3 4"
rgb(18, 58, 188) rgb(242, 240, 237)
// 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)
``` ```
(Don't forget to use the `-h` or `--help` flags for in-terminal help) (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 # Conversion Sources
Hex<->RGB: Hex<->RGB:
+9 -8
View File
@@ -6,7 +6,7 @@ TYPES = ['hex', 'rgb', 'cmy', 'cmyk', 'hsl', 'hsv']
HEX_LETTERS = ['a', 'b', 'c', 'd', 'e', 'f'] HEX_LETTERS = ['a', 'b', 'c', 'd', 'e', 'f']
OUTPUT = 'stdout' OUTPUT_DESTINATION = 'stdout'
VERBOSE = False VERBOSE = False
@@ -54,10 +54,10 @@ def main():
# determine if output should be (over?)written to file # determine if output should be (over?)written to file
if args.output : if args.output :
global OUTPUT global OUTPUT_DESTINATION
OUTPUT = args.output OUTPUT_DESTINATION = args.output
if not args.append : if not args.append :
output_file = open(OUTPUT, 'w') output_file = open(OUTPUT_DESTINATION, 'w')
output_file.write('') output_file.write('')
output_file.close() 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. # If string contains a valid Hex color code, it gets returned as a string.
def validateHex(value) : def validateHex(value) :
# attempt to extract hex code # attempt to extract hex code
hexcode = extractValues(value, 1, True)[0] hexcode = extractValues(value, 1, True)
if not hexcode : if not hexcode :
print('ERROR: Improper format for hex code (see --help)') print('ERROR: Improper format for hex code (see --help)')
return return
return hexcode return hexcode[0]
# Takes in a string. Returns same list as integers if valid RGB values. # Takes in a string. Returns same list as integers if valid RGB values.
def validateRGB(color) : def validateRGB(color) :
@@ -460,6 +460,7 @@ def validateRGB(color) :
rgbValues = extractValues(color, 3) rgbValues = extractValues(color, 3)
if not rgbValues : if not rgbValues :
print('bo')
return return
intValues = [] intValues = []
@@ -633,7 +634,7 @@ def printConversions(convertedValues) :
output.append(colorCode) output.append(colorCode)
# deliver formatted output # deliver formatted output
if OUTPUT != 'stdout' : if OUTPUT_DESTINATION != 'stdout' :
''' '''
TODO FOR EFFICIENCY: 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 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. 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 : for color in output :
file.write(color + '\n') file.write(color + '\n')
file.write('\n') file.write('\n')