/
colorConvert.py
executable file
·364 lines (305 loc) · 11 KB
/
colorConvert.py
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
#!/usr/bin/env python3
#
# colorConvert.py: Normalize various color specs.
# 2016-04-14: Written by Steven J. DeRose.
#
import sys
import os
import re
import math
import codecs
import colorsys
import logging
import webcolors
lg = logging.getLogger()
palColors = {}
__metadata__ = {
"title" : "colorConvert",
"description" : "Normalize various color specs.",
"rightsHolder" : "Steven J. DeRose",
"creator" : "http://viaf.org/viaf/50334488",
"type" : "http://purl.org/dc/dcmitype/Software",
"language" : "Python 3.7",
"created" : "2016-04-14",
"modified" : "2021-06-24",
"publisher" : "http://github.com/sderose",
"license" : "https://creativecommons.org/licenses/by-sa/3.0/"
}
__version__ = __metadata__["modified"]
descr = """
=Description=
[not quite finished]
Normalize colors to a given form from a variety of other forms.
Forms accepted:
#RGB
#RRGGBB
#RRRGGGBBB
#RRRRGGGGBBBB
rgb(r, g, b)
rgb(r, g, b, h)
hsv(h, s, v))
hsva(h, s, v, a)
yiq(y, i, q)
HTML and CSS color names
Arguments to the function-style forms may be specified as any of:
decimal integers from 0 to 255
hexadecimal numbers prefixed with "0x", from 0x00 to 0xff
octal numbers with a leading "0"
decimal non-integers from 0 to 255
decimal numbers followed by a percent sign ("%")
'''Note''': If some input cannot be parsed,
a ''ValueError'' exception is raised.
=Related Commands=
Pip packages: colorsys, webcolors [https://pypi.python.org/pypi/webcolors/1.3].
Pantone conversion is said to be supported
by [https://pypi.python.org/pypi/pycolorname].
`python-colormath` supports many color spaces, but it's ''big''.
See ''https://python-colormath.readthedocs.org/en/latest/].
=Known bugs and limitations=
Although the rgba() and hsva() forms are accepted, the alpha (transparency)
component is discarded.
CMYK, spot color systems, and many other possibilities are not supported.
Conversion I<to> named HTML colors is not provided (yet).
A feature to pick, for each input color, the nearest color from a given
pal*[ae]t*e*, would be helpful.
=References=
[http://www.w3.org/TR/css3-color/#SRGB]
[https://en.wikipedia.org/wiki/CMYK_color_model#Conversion]
=History=
2016-04-14: Written. Copyright by Steven J. DeRose.
2018-04-18: lint.
2020-03-03, 2021-06-24: New layout.
=Rights=
Copyright 2016-04-14 by Steven J. DeRose. This work is licensed under a
Creative Commons Attribution-Share-alike 3.0 unported license.
For further information on this license, see
[https://creativecommons.org/licenses/by-sa/3.0].
For the most recent version, see [http://www.derose.net/steve/utilities]
or [https://github.com/sderose].
=Options=
"""
knownSchemes = [ 'rgb', 'rgba', 'hsv', 'hsva', 'yiq', 'hls' ]
token = r'\s*([\da-fA-F.]+%?)'
try:
fe = r'(\w+)\(%s,%s,%s(,%s)?\)' % (token,token,token,token)
print("Expr: /%s/'", fe)
functionExpr = re.compile(fe)
except re.error as e0:
print("Bad regex: '%s'.\n %s", fe, e0)
sys.exit()
###############################################################################
#
def doOneFile(fh, path):
"""Read and deal with one individual file.
"""
recnum = 0
rec = ""
while (True):
try:
rec = fh.readline()
except IOError as e:
lg.error("Error (%s) reading record %d of '%s'.",
type(e), recnum, path)
break
if (len(rec) == 0): break # EOF
recnum += 1
rec = rec.rstrip()
rgbTriple = cconvert(rec)
outColor = serialize(rgbTriple, args.out)
if (args.pal):
#nearest = findNearestPalColor(serialize(rgbTriple, 'rgb6'))
#outColor += '\t Nearest: %s' % (nearest)
lg.error("PAL not yet supported.")
sys.exit()
print(outColor)
fh.close()
return(recnum)
def cconvert(s):
# Try HTML and CSS names. 'webcolors' returns as $RRGGBB.
rgbString = None
if (s in webcolors.HTML4_NAMES_TO_HEX):
rgbString = webcolors.HTML4_NAMES_TO_HEX[s]
#print("Found '%s' in HTML4 colors. Got: %s.", s, rgbString)
elif (s in webcolors.CSS2_NAMES_TO_HEX):
rgbString = webcolors.CSS2_NAMES_TO_HEX[s]
#print("Found '%s' in CSS2 colors. Got: %s.", s, rgbString)
elif (s in webcolors.CSS21_NAMES_TO_HEX):
rgbString = webcolors.CSS21_NAMES_TO_HEX[s]
#print("Found '%s' in CSS2.1 colors. Got: %s.", s, rgbString)
elif (s in webcolors.CSS3_NAMES_TO_HEX):
rgbString = webcolors.CSS3_NAMES_TO_HEX[s]
#print("Found '%s' in CSS3 colors. Got: %s.", s, rgbString)
if (rgbString is not None):
s = rgbString
# Try functional notations, like 'rgb(12, 50%, 0xA0)'
mat = re.match(functionExpr, s)
if (mat):
#print("functional")
func = mat.group(1)
a1 = convertNumber(mat.group(2))
a2 = convertNumber(mat.group(3))
a3 = convertNumber(mat.group(4))
# TODO: Do something with alpha....
# if (mat.group(5)): alpha = convertNumber(mat.group(5))
# else: alpha = 0
if (func == 'rgb' or func == 'rgba'):
rgbTriple = [ a1, a2, a3 ]
elif (func == 'hls'):
rgbTriple = colorsys.hls_to_rgb(a1, a2, a3)
elif (func == 'hsv'):
rgbTriple = colorsys.hsv_to_rgb(a1, a2, a3)
elif (func == 'yiq'):
rgbTriple = colorsys.yiq_to_rgb(a1, a2, a3)
else:
raise ValueError('Unrecognized scheme "%s".' % (func))
# Try the #RRGGBB types (including re-processing ones from names!)
elif (s.startswith('#')):
if (len(s) % 3 != 1):
raise ValueError('Bad length for #rgb color.')
per = int(len(s)/3)
#print("rgb%d" % (per))
rgbTriple = [ ]
for i in range(3):
piece = s[per*i+1:per*i+per+1]
if (len(piece)==1): piece += piece
rgbTriple.append(convertNumber('0x' + piece))
else:
raise ValueError('Unrecognized syntax: "%s".' % (s))
return(rgbTriple)
def convertNumber(s:str):
"""Take various numeric forms, and return a float in 0..1.
"""
#print("converting: '%s'." % (s))
try:
if (s.endswith('%')): return(float(s[0:-1])/100)
if (s.startswith('0x')): return(int(s[2:],16)/255.0)
if (s.startswith('0')): return(int(s,8)/255.0)
if ('.' in s): return(float(s))
return(float(int(s)/255.0))
except ValueError as e:
print("Cannot parse number from '%s'.\n %s" % (s, e))
sys.exit()
def serialize(rgb, fmt):
"""Convert a tuple of floats to the named output format.
"""
if (fmt == 'rgb3'):
return('#%01x%01x%01x' %
(int(rgb[0]*15), int(rgb[1]*15), int(rgb[2]*15)))
elif (fmt == 'rgb6'):
return('#%02x%02x%02x' %
(int(rgb[0]*255), int(rgb[1]*255), int(rgb[2]*255)))
elif (fmt == 'rgb9'):
return('#%03x%03x%03x' %
(int(rgb[0]*4095), int(rgb[1]*4095), int(rgb[2]*4095)))
elif (fmt == 'rgdDec'):
return('rgb(%3d, %3d, %3d)' %
(int(rgb[0]*255), int(rgb[1]*255), int(rgb[2]*255)))
elif (fmt == 'rgb%'):
return('rgb(%5.1f%%, %5.1f%%, %5.1f%%)' %
(rgb[0], rgb[1], rgb[2]))
elif (fmt == 'hsv'):
return('hsv(%5.3f%%, %5.3f%%, %5.3f%%)' %
colorsys.rgb_to_hsv(rgb[0], rgb[1], rgb[2]))
elif (fmt == 'hls'):
return('hls(%5.1f%%, %5.1f%%, %5.1f%%)' %
colorsys.rgb_to_hls(rgb[0], rgb[1], rgb[2]))
elif (fmt == 'yiq'):
return('yiq(%5.1f%%, %5.1f%%, %5.1f%%)' %
colorsys.rgb_to_yiq(rgb[0], rgb[1], rgb[2]))
else:
raise ValueError('Unknown output format "%s".' % (fmt))
def cdistance(rgb1, rgb2):
tot = 0.0
for i in range(len(rgb1)):
tot += (rgb1[i]-rgb2[i])**2
return(math.sqrt(tot))
#def findNearestPalColor(rgb6):
# minDist = 999
# minRGB = None
# for i in range(len(pal)):
# thisDist = cdistance(rgb5, pal[i])
# if (abs(thisDist) < abs(minDict)):
# minDict = thisDict
# minRGB = pal[i]
# return(minRGB)
###############################################################################
# Main
#
def processOptions():
import argparse
try:
from BlockFormatter import BlockFormatter
parser = argparse.ArgumentParser(
description=descr, formatter_class=BlockFormatter)
except ImportError:
parser = argparse.ArgumentParser(description=descr)
parser.add_argument(
"--color", # Don't default. See below.
help='Colorize the output.')
parser.add_argument(
"--iencoding", type=str, metavar='E', default="utf-8",
help='Assume this character set for input files. Default: utf-8.')
parser.add_argument(
"--oencoding", type=str, metavar='E',
help='Use this character set for output files.')
parser.add_argument(
"--out", type=str, default='rgb6', choices=
[ 'rgb3', 'rgb6', 'rgb9', 'rgbdec', 'rgb%', 'hsv', 'hsl', 'yiq', 'name' ],
help='Suppress most messages.')
parser.add_argument(
"--pal", type=str,
help='File of "known" colors, one per line as #RRGGBB.')
parser.add_argument(
"--quiet", "-q", action='store_true',
help='Suppress most messages.')
parser.add_argument(
"--unicode", action='store_const', dest='iencoding',
const='utf8', help='Assume utf-8 for input files.')
parser.add_argument(
"--verbose", "-v", action='count', default=0,
help='Add more messages (repeatable).')
parser.add_argument(
"--version", action='version', version=__version__,
help='Display version information, then exit.')
parser.add_argument(
'files', type=str, nargs=argparse.REMAINDER,
help='Path(s) to input file(s)')
args0 = parser.parse_args()
if (lg and args0.verbose):
logging.basicConfig(level=logging.INFO - args0.verbose,
format="%(message)s")
if (args0.color is None):
args0.color = ("CLI_COLOR" in os.environ and sys.stderr.isatty())
lg.setColors(args0.color)
return(args0)
args = processOptions()
if (args.pal):
try:
pfh = codecs.open(args.pal, mode='r', encoding=args.iencoding)
except IOError:
lg.error("Can't open -pal file '%s'.", args.pal)
sys.exit()
precnum = 0
while (True):
rec0 = pfh.readline()
if (rec0==""): break
precnum += 1
rec0 = rec0.trim()
if (not re.match('#[0-9a-fA-F]{6,6}', rec0)):
lg.error("%s:%d: Bad record: '%s'.", args.pal, precnum, rec0)
sys.exit()
else:
palColors[rec0] = 1
pfh.close()
if (not args.files):
if (not args.quiet): print("Waiting on STDIN...")
doOneFile(sys.stdin, 'STDIN')
else:
for f in (args.files):
try:
fh0 = codecs.open(f, mode='r', encoding=args.iencoding)
except IOError:
lg.error("Can't open '%s'.", f)
sys.exit()
doOneFile(fh0, f)
fh0.close()