summaryrefslogtreecommitdiffstats
path: root/src/colour.py
blob: 69d3064a99a83cc3ac476dd1ce07054a13972332 (plain) (blame)
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
#!/usr/bin/env python3

# Copyright © 2014  Mattias Andrée (maandree@member.fsf.org)
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
# 
# 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/>.


def linear_to_standard(*colour):
    '''
    Convert [0, 1] linear RGB to [0, 1] sRGB
    
    @param   colour:*float           The red component, the green component, and the blue component
    @return  :[float, float, float]  The red, green and blue components
    '''
    return [12.92 * c if c <= 0.0031308 else (1 + 0.055) * c ** (1 / 2.4) - 0.055 for c in colour]


def standard_to_linear(*colour):
    '''
    Convert [0, 1] sRGB to linear [0, 1] RGB
    
    @param   colour:*float           The red component, the green component, and the blue component
    @return  :[float, float, float]  The red, green and blue components
    '''
    return [c / 12.92 if c <= 0.04045 else ((c + 0.055) / (1 + 0.055)) ** 2.4 for c in colour]


def ciexyy_to_ciexyz(x, y, Y):
    '''
    Convert CIE xyY to CIE XYZ
    
    @param   x:float                 The x parameter
    @param   y:float                 The y parameter
    @param   Y:float                 The Y parameter
    @return  :[float, float, float]  The X, Y and Z parameters
    '''
    return [Y if y == 0 else Y * x / y, Y, Y if y == 0 else Y * (1 - x - y) / y]


def ciexyz_to_ciexyy(X, Y, Z):
    '''
    Convert CIE XYZ to CIE xyY
    
    @param   X:float                 The X parameter
    @param   Y:float                 The Y parameter
    @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]


def ciexyz_to_linear(X, Y, Z):
    '''
    Convert CIE XYZ to [0, 1] linear RGB
    
    @param   X:float                 The X parameter
    @param   Y:float                 The Y parameter
    @param   Z:float                 The Z parameter
    @return  :[float, float, float]  The red, green and blue components
    '''
    r =  3.24156 * X - 1.53767 * Y - 0.49870 * Z
    g = -0.96920 * X + 1.87589 * Y + 0.04155 * Z
    b =  0.05562 * X - 0.20396 * Y + 1.05686 * Z
    return [r, g, b]


def linear_to_ciexyz(r, g, b):
    '''
    Convert [0, 1] linear RGB to CIE XYZ
    
    @param   r:float                 The red component
    @param   g:float                 The green component
    @param   b:float                 The blue component
    @return  :[float, float, float]  The X, Y and Z parameters
    '''
    X = 0.4123160 * r + 0.3576020 * g + 0.1805010 * b
    Y = 0.2126000 * r + 0.7151990 * g + 0.0722016 * b
    Z = 0.0193297 * r + 0.1192040 * g + 0.9506340 * b
    return [X, Y, Z]


def srgb_to_ciexyy(r, g, b):
    '''
    Convert [0, 1] sRGB to CIE xyY
    
    @param   r:float                 The red component
    @param   g:float                 The green component
    @param   b:float                 The blue component
    @return  :[float, float, float]  The x, y and Y parameters
    '''
    if r == g == b == 0:
        return (0.312857, 0.328993, 0)
    (r, g, b) = standard_to_linear(r, g, b)
    (X, Y, Z) = linear_to_ciexyz(r, g, b)
    (x, y, Y) = ciexyz_to_ciexyy(X, Y, Z)
    return (x, y, Y)


def ciexyy_to_srgb(x, y, Y):
    '''
    Convert CIE xyY to [0, 1] sRGB
    
    @param   x:float                 The x parameter
    @param   y:float                 The y parameter
    @param   Y:float                 The Y parameter
    @return  :[float, float, float]  The red, green and blue components
    '''
    (X, Y, Z) = ciexyy_to_ciexyz(x, y, Y)
    (r, g, b) = ciexyz_to_linear(X, Y, Z)
    (r, g, b) = linear_to_standard(r, g, b)
    return (r, g, b)