-
Notifications
You must be signed in to change notification settings - Fork 2
Description
Some scripts that parse sRGB hex color codes from CLI switches or files have graceless handling of a prefix # to the color code (either requiring it be present or not present and breaking in the other case). The below code fixes that for Python (bash scripts, if there are bash scripts that do this also, are another concern).
Mind this is bash nesting Python; code that handles it more gracefully from that script is:
pattern = r'#[0-9a-fA-F]{6}'
# Load sRGB hex colors from sourcePaletteFileName by pattern match (allows comments and other things in file) :
colors = []
with open(\"$sourceFileName\", \"r\") as file_list:
for line in file_list:
match = re.search(pattern, line)
if match:
append_str = str(match.group())
# convert to lowercase, to avoid a \"ValueError: list.remove(x): x not in list error\" if there are mixed cases in the source data:
append_str = append_str.lower()
colors.append(append_str)
file_list.close()
# for color in colors:
# print(color)
# - make an empty intended final list (a list to build)
finalList = []
# - set first color in list as compare color IF no color specified for start
# quasi-import value from bash here, very weirdly; I think this is code smell; it only works if I use escaped double quote marks \" around the bash variable:
if \"$searchColor\" != '':
pySearchColor = \"$searchColor\"
else:
pySearchColor = colors[0]
# strip any gunk around that and reduce it to only six hex digits; throw an error and exit if its rong format:
# Regex to match exactly 6 consecutive hex digits (case insensitive)
found = re.search(r'[0-9a-fA-F]{6}', pySearchColor)
# If a match was not found, throw an error and exit. It would be nice to play the Resetty music here.
if not found:
print(\"\nERROR: -s | --startcolor or first color\", pySearchColor, \"from input file not a valid sRGB hex color code. Exit 2.\n\")
sys.exit(2)
else:
pySearchColor = \"#\" + found.group(0)
scripts that could use this if I don't deprecate them include:
RGBhexColorSortToDarksAndLightsInCIECAM02.py
RGBhexColorSortInCIECAM02.py
RGBhexColorSortInCAM16-UCS.py
NOTE: colors[0] IN THE ABOVE may be an array from possible multiple matches on a line. Handle that, to adhere to the .hexplt spec support for that. Similar situation for the regex result found.group(0).
Metadata
Metadata
Assignees
Labels
Projects
Status