summaryrefslogtreecommitdiffstats
path: root/src/colour.py
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2014-03-27 23:59:39 +0100
committerMattias Andrée <maandree@operamail.com>2014-03-27 23:59:39 +0100
commitbe19ac7cb8638d9a751f5ea32e2722d99604af85 (patch)
treed1b40cf570ff250c122e4e0981fbb06589963076 /src/colour.py
parentdoc + make it possible to select download command for weather (diff)
downloadblueshift-be19ac7cb8638d9a751f5ea32e2722d99604af85.tar.gz
blueshift-be19ac7cb8638d9a751f5ea32e2722d99604af85.tar.bz2
blueshift-be19ac7cb8638d9a751f5ea32e2722d99604af85.tar.xz
doc
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src/colour.py')
-rw-r--r--src/colour.py40
1 files changed, 16 insertions, 24 deletions
diff --git a/src/colour.py b/src/colour.py
index e0cc490..e2bbc01 100644
--- a/src/colour.py
+++ b/src/colour.py
@@ -15,6 +15,9 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
+# This module implements functions from convertions between colour spaces
+# and comparion of colours
+
def linear_to_standard(*colour):
'''
@@ -57,11 +60,8 @@ def ciexyz_to_ciexyy(X, Y, Z):
@param Z:float The Z parameter
@return :[float, float, float] The x, y and Y parameters
'''
- if X + Y + Z == 0:
- return [0, 0, 0]
- y = Y / (X + Y + Z)
- x = X / (X + Y + Z)
- return [x, y, Y]
+ s = X + Y + Z
+ return [X / s, Y / s, Y] if not s == 0 else [0, 0, 0]
def matrix_mul_vector(matrix, vector):
@@ -78,6 +78,9 @@ def matrix_mul_vector(matrix, vector):
ciexyz_to_linear_matrix = [[ 3.240450, -1.537140, -0.4985320],
[-0.969266, 1.876010, 0.0415561],
[0.0556434, -0.204026, 1.0572300]]
+'''
+Multiplication matrix to convert from CIE xyY to linear RGB
+'''
def ciexyz_to_linear(X, Y, Z):
'''
@@ -94,6 +97,9 @@ def ciexyz_to_linear(X, Y, Z):
linear_to_ciexyz_matrix = [[0.4124564, 0.3575761, 0.1804375],
[0.2126729, 0.7151522, 0.0721750],
[0.0193339, 0.1191920, 0.9503041]]
+'''
+Multiplication matrix to convert from linear RGB to CIE xyY
+'''
def linear_to_ciexyz(r, g, b):
'''
@@ -118,10 +124,7 @@ def srgb_to_ciexyy(r, g, b):
'''
if r == g == b == 0:
return (0.312857, 0.328993, 0)
- rc = standard_to_linear(r, g, b)
- rc = linear_to_ciexyz(*rc)
- rc = ciexyz_to_ciexyy(*rc)
- return rc
+ return ciexyz_to_ciexyy(*linear_to_ciexyz(*standard_to_linear(r, g, b)))
def ciexyy_to_srgb(x, y, Y):
@@ -133,10 +136,7 @@ def ciexyy_to_srgb(x, y, Y):
@param Y:float The Y parameter
@return :[float, float, float] The red, green and blue components
'''
- rc = ciexyy_to_ciexyz(x, y, Y)
- rc = ciexyz_to_linear(*rc)
- rc = linear_to_standard(*rc)
- return rc
+ return linear_to_standard(*ciexyz_to_linear(*ciexyy_to_ciexyz(x, y, Y)))
def ciexyz_to_cielab(x, y, z):
@@ -170,10 +170,7 @@ def cielab_to_xiexyz(l, a, b):
x = a / 500 + y
z = y - b / 200
f = lambda c : c ** 3 if c ** 3 > 0.00885642 else (c - 0.1379310) / (7.78 + 703 / 99900)
- (x, y, z) = [f(c) for c in (x, y, z)]
- x *= 0.95047
- z *= 1.08883
- return (x, y, z)
+ return [f(c) * m for c, m in zip((x, y, z), (0.95047, 1, 1.08883))]
def delta_e(a, b):
@@ -184,11 +181,6 @@ def delta_e(a, b):
@param b:(float, float, float) The second colour
@return :float The difference
'''
- a = standard_to_linear(*a)
- b = standard_to_linear(*b)
- a = linear_to_ciexyz(*a)
- b = linear_to_ciexyz(*b)
- a = ciexyz_to_cielab(*a)
- b = ciexyz_to_cielab(*b)
- return sum([(c1 - c2) ** 2 for c1, c2 in zip(a, b)]) ** 0.5
+ standard_to_cielab = lambda x : ciexyz_to_cielab(*linear_to_ciexyz(*standard_to_linear(*a)))
+ return sum([(c1 - c2) ** 2 for c1, c2 in zip(standard_to_cielab(a), standard_to_cielab(b))]) ** 0.5