diff --git a/README.md b/README.md index 41e8cf2..b1d53cf 100644 --- a/README.md +++ b/README.md @@ -1,49 +1,55 @@ -# TODO - -fix extractor, get hex working - # color-converter A CLI utility written in Python that translates color codes to other formats. = actively under construction = +# 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. + # Usage -Download the python file and run it! Requires nothing besides vanilla Python :) +This program supports hexcodes, RGB, CMY, CMYK, HSL, and HSV. -I'll flesh 'usage' README out later, but for now, use the help menu: -`python color-converter.py --help` +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. -# Project Goals +For example, a simple input with all conversions: -I started this project because I found myself converting between Hex and RGB a lot while ricing my Arch setup, which got me thinking about how all of these conversion websites work. +``` +$ python color-converter.py "#85feab" +#85feab +rgb(133, 254, 171) +cmy(47.84, 0.39, 32.94) +cmyk(47.64, 0.00, 32.68, 0.39) +hsl(138.84, 98.37, 75.88) +hsv(138.84, 47.64, 99.61) +``` -Using any random color-picker online to go from RBG->Hex (and vice versa) works plenty fine, but I started to wonder if there was a simple program to do said conversions offline. Something that was fast, doesn't require internet/no ads, etc. I poked around on the AUR for any sort of CLI color converting utility, and couldn't find anything... At this point, I realized it was up to me! +You can use flags to limit the output: -# Roadmap +``` +$ python color-converter.py -cmy -hsv "#85feab" +cmy(47.84, 0.39, 32.94) +hsv(138.84, 47.64, 99.61) +``` -I originally just wanted this to be an exercise in Python that teaches me about color conversions and can do HEX <-> RGB. I've since decided to flesh it out and try to make it into something that could prove useful. My goal is to polish and post this around in the hopes other people find it helpful. +--- -TODO: +It can also function as a color translation utility for converting colors in bulk. -- automatic format detection, no longer requiring a flag to indicate what format input is -- accept more than one color code at a time (-f for file input, -l for multiple (list of) strings -- (to expand on ^) accept input from text file, where each line is it's own color code to convert -- add flags to control which conversions are output (in the case you only want a single conversion) and how (do you want just the raw values or the whole string for with commas and stuff?) -- combine the last two bullet points and boom: now the tool could take a whole list of colors in X format and spit out a file where every color is in Y format -- (to expand on ^) if we had automatic format detection and formatting output flags, you could pass in a whole file of any color formats and standardize them to a given format. -- finish the color value extractor - -# ENVISIONED USAGE: - -in its simplest form, this program takes in a color code and prints out all of the equivalent color codes in the different supported formats (Hex, RGB, CMY, CMYK, HSL, HSV). - -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) and you can limit the output to one or more specific formats. +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. -TODO: UPDATE THIS EXAMPLE -For example, input this file: +For example, use this command: + +``` +$ python color-converter -cmyk -i "input_file" -o "output_file" +``` + +where the input file contains: ``` rgb(1, 2, 3) @@ -51,7 +57,7 @@ rgb(1, 2, 3) HSV(1, 2, 3) ``` -and receive this file: +and the output file is populated with: ``` cmyk(66.67%, 33.33%, 0%, 98.82%) @@ -59,38 +65,44 @@ cmyk(79.07%, 39.53%, 0%, 66.27%) cmyk(0%, 1.97%, 2%, 97%) ``` -with this command: +_Note: include the `-a` flag to append to the specified output file, rather than overwriting it._ -> `$ python color-converter -cmyk -i input_file -o output_file` +--- -_FURTHER TIPS / EXPLANATION:_ +The program attempts to determine what color format you're inputting, and will succeed as long as the format is indicated at some point in the input string. -From the command line, the program accepts input colors as strings wrapped in quotes. The sanitization is pretty robust and unopinionated when it comes to formatting, as long as the format is indicated at some point in the string. +For example, these are all valid strings to input 100, 200, and 300 as RGB values: -> Example valid RGB inputs: -> `$ python color-converter "rgb(1, 2, 3)"` -> `$ python color-converter "rgb(1 2 3)"` -> `$ python color-converter "rgb1 2 3"` +``` +"rgb(100, 200, 300)" +" rgb: 100 200 300" +"rGb-100 . 200^300" +``` -The program will always try to automatically determine what format you entered. +_Note: Hexcodes will require a '#' in order to be automatically recognized._ -> The sole exception to this is if you specify the input format. This is done when passing the program integers rather than strings. For example, to pass in raw RGB inputs and convert them to HSL: -> `$ python color-converter -rgb -hsl 1 2 3` +While the program will always try to auto-detect the input format, If you only have raw number values, you can specify a fallback format with flags. -- If you have multiple colors to convert, it may be faster to put them in a file than individually wrap them in quotes. +For example: -- If you're only inputting one value, you can +``` +$ python color-converter.py -rgb -isHex "123abc" +rgb(18, 58, 188) -_For a single input to all available formats_: -`$ python color-converter "input_color"` +// or -_For a single input to a specific format_: -`$ python color-converter -output_format "input_color"` +$ 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) +``` -_For input(s) from command line to all available formats_: -`$ python color-converter "input_color1" "input_color2" "input_color3"` +(Don't forget to use the `-h` or `--help` flags for in-terminal help) -## Conversion Sources +# Conversion Sources Hex<->RGB: diff --git a/color-converter.py b/color-converter.py index 324d271..9f5620d 100644 --- a/color-converter.py +++ b/color-converter.py @@ -349,7 +349,6 @@ def RGBtoHSVorHSL(rgbValues, convertTo) : chroma = xMax - xMin # Convert by following formula - value = xMax lightness = (xMax + xMin) / 2 @@ -501,9 +500,11 @@ def extractValues(color, numValues, isHex = False) : while i < len(color) : if color[i].isnumeric() or color[i] == '.' : tempValue += color[i] - elif len(tempValue) > 0 : + elif len(tempValue) > 0 and tempValue != '.' : extractedValues.append(tempValue) tempValue = '' + else : + tempValue = '' if len(extractedValues) == numValues : break i = i + 1