/usr/lib/python3/dist-packages/defcon/objects/color.py is in python3-defcon 0.3.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | from fontTools.misc.py23 import basestring
class Color(str):
"""
This object represents a color. This object is immutable.
The initial argument can be either a color string as defined in the UFO
specification or a sequence of (red, green, blue, alpha) components.
By calling str(colorObject) you will get a UFO compatible color string.
You can also iterate over the object to create a sequence::
colorTuple = tuple(colorObject)
"""
def __new__(self, value):
# convert from string
if isinstance(value, basestring):
value = _stringToSequence(value)
r, g, b, a = value
# validate the values
color = (("r", r), ("g", g), ("b", b), ("a", a))
for component, v in color:
if v < 0 or v > 1:
raise ValueError("The color for %s (%s) is not between 0 and 1." % (component, str(v)))
# convert back to a normalized string
r = _stringify(r)
g = _stringify(g)
b = _stringify(b)
a = _stringify(a)
s = ",".join((r, g, b, a))
# call the super
return super(Color, self).__new__(Color, s)
def __iter__(self):
value = _stringToSequence(self)
return iter(value)
def _get_r(self):
return _stringToSequence(self)[0]
r = property(_get_r, "The red component.")
def _get_g(self):
return _stringToSequence(self)[1]
g = property(_get_g, "The green component.")
def _get_b(self):
return _stringToSequence(self)[2]
b = property(_get_b, "The blue component.")
def _get_a(self):
return _stringToSequence(self)[3]
a = property(_get_a, "The alpha component.")
def _stringToSequence(value):
r, g, b, a = [i.strip() for i in value.split(",")]
value = []
for component in (r, g, b, a):
try:
v = int(component)
value.append(v)
continue
except ValueError:
pass
v = float(component)
value.append(v)
return value
def _stringify(v):
"""
>>> _stringify(1)
'1'
>>> _stringify(.1)
'0.1'
>>> _stringify(.01)
'0.01'
>>> _stringify(.001)
'0.001'
>>> _stringify(.0001)
'0.0001'
>>> _stringify(.00001)
'0.00001'
>>> _stringify(.000001)
'0'
>>> _stringify(.000005)
'0.00001'
"""
# it's an int
i = int(v)
if v == i:
return str(i)
# it's a float
else:
# find the shortest possible float
for i in range(1, 6):
s = "%%.%df" % i
s = s % v
if float(s) == v:
break
# see if the result can be converted to an int
f = float(s)
i = int(f)
if f == i:
return str(i)
# otherwise return the float
return s
if __name__ == "__main__":
import doctest
doctest.testmod()
|