summaryrefslogtreecommitdiffstats
path: root/src/curve.py
blob: d3e2a2d4422111c720dcdcd12045eeb1b71b818c (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
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
#!/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/>.
import math

from colour import *



# /usr/share/blueshift
DATADIR = '.'

# Mapping input and output maximum values + 1
i_size = 2 ** 8
o_size = 2 ** 16

# Red, green and blue curves
r_curve = [i / (i_size - 1) for i in range(i_size)]
g_curve = [i / (i_size - 1) for i in range(i_size)]
b_curve = [i / (i_size - 1) for i in range(i_size)]




clip_result = True
'''
Set to `False` if you want to allow value overflow rather than clipping,
doing so can create visual artifacts
'''


def curves(r, g, b):
    '''
    Generate a tuple of curve–parameter pairs
    
    @param   r  The red parameter
    @param   g  The green parameter
    @param   b  The blue parameter
    @return     `((r_curve, r), (g_curve, g), (b_curve, b))`
    '''
    return ((r_curve, r), (g_curve, g), (b_curve, b))



def series_d(temperature):
    '''
    Calculate the colour for a blackbody temperature
    
    @param   temperature:float       The blackbody temperature in kelvins, must be inside [4000, 7000]
    @return  :(float, float, float)  The red, green and blue components of the white point
    '''
    x = 0
    ks = ((0.244063, 0), (0.09911, 1), (2.9678, 2), (-4.6070, 3))
    if temperature > 7000:
        ks = ((0.237040, 0), (0.24748, 1), (1.9018, 2), (-2.0064, 3))
    for (k, d) in ks:
        x += k * 10 ** (d * 3) / temperature ** d
    y = 2.870 * x - 3.000 * x ** 2 - 0.275
    return ciexy_to_srgb(x, y, 1.0)


def simple_whitepoint(temperature):
    '''
    Calculate the colour for a blackbody temperature using a simple, but inaccurate, algorithm
    
    @param   temperature:float       The blackbody temperature in kelvins, not guaranteed for values outside [1000, 40000]
    @return  :(float, float, float)  The red, green and blue components of the white point
    '''
    r, g, b = 1, 1, 1
    temp = temperature / 100
    if temp > 66:
        temp -= 60
        r = 1.292936186 * temp ** 0.1332047592
        g = 1.129890861 * temp ** -0.0755148492
    else:
        g = 0.390081579 * math.log(temp) - 0.631841444
        if temp <= 19:
            b = 0
        elif temp < 66:
            b = 0.543206789 * math.log(temp - 10) - 1.196254089
    return (r, g, b)


cmf_2deg_cache = None
def cmf_2deg(temperature):
    '''
    Calculate the colour for a blackbody temperature using raw CIE 1931 2 degree CMF data with interpolation
    
    @param   temperature:float       The blackbody temperature in kelvins, clipped to [1000, 40000]
    @return  :(float, float, float)  The red, green and blue components of the white point
    '''
    if cmf_2deg_cache is None:
        with open(DATADIR + '/2deg', 'rb') as file:
            cmf_2deg_cache = file.read()
        cmf_2deg_cache.decode('utf-8', 'error').split('\n')
        cmf_2deg_cache = [[float(x) for x in x_y.split(' ')] for x_y in cmf_2deg_cache]
    temperature = min(max(0, temperature), 1000)
    x, y = 0, 0
    if (temp % 100) == 0:
        (x, y) = temperature[(temp - 1000) // 100]
    else:
        temp -= 1000
        (x1, y1) = temperature[temp // 100]
        (x2, y2) = temperature[temp // 100 + 1]
        temp = (temp % 100) / 100
        x = x1 * temp + x2 * (1 - temp)
        y = y1 * temp + y2 * (1 - temp)
    return ciexy_to_srgb(x, y, 1.0)


cmf_10deg_cache = None
def cmf_10deg(temperature):
    '''
    Calculate the colour for a blackbody temperature using raw CIE 1964 10 degree CMF data with interpolation
    
    @param   temperature:float       The blackbody temperature in kelvins, clipped to [1000, 40000]
    @return  :(float, float, float)  The red, green and blue components of the white point
    '''
    if cmf_2deg_cache is None:
        with open(DATADIR + '/10deg', 'rb') as file:
            cmf_2deg_cache = file.read()
        cmf_2deg_cache.decode('utf-8', 'error').split('\n')
        cmf_2deg_cache = [[float(x) for x in x_y.split(' ')] for x_y in cmf_2deg_cache]
    temperature = min(max(0, temperature), 1000)
    x, y = 0, 0
    if (temp % 100) == 0:
        (x, y) = temperature[(temp - 1000) // 100]
    else:
        temp -= 1000
        (x1, y1) = temperature[temp // 100]
        (x2, y2) = temperature[temp // 100 + 1]
        temp = (temp % 100) / 100
        x = x1 * temp + x2 * (1 - temp)
        y = y1 * temp + y2 * (1 - temp)
    return ciexy_to_srgb(x, y, 1.0)



def temperature(temperature, algorithm, linear_rgb = True):
    '''
    Change colour temperature according to the CIE illuminant series D
    
    @param  temperature:float                        The blackbody temperature in kelvins
    @param  algorithm:(float)→(float, float, float)  Algorithm for calculating a white point, for example `series_d` or `simple_whitepoint`
    @param  linear_rgb:[bool]                        Whether to use linear RGB, otherwise sRG is used
    '''
    if temperature == 6500:
        return
    (r, g, b) = algorithm(temperature)
    if linear_rgb:
        for curve in (r_curve, g_curve, b_curve):
            for i in range(i_size):
                R, G, B = r_curve[i], g_curve[i], b_curve[i]
                (R, G, B) = standard_to_linear(R, G, B)
                r_curve[i], g_curve[i], b_curve[i] = R, G, B
    rgb_brightness(r, g, b)
    if linear_rgb:
        for curve in (r_curve, g_curve, b_curve):
            for i in range(i_size):
                R, G, B = r_curve[i], g_curve[i], b_curve[i]
                (R, G, B) = linear_to_standard(R, G, B)
                r_curve[i], g_curve[i], b_curve[i] = R, G, B


def divide_by_maximum():
    '''
    Divide all colour components by the value of the most prominent colour component for each colour
    '''
    for i in range(i_size):
        m = max([abs(x) for x in (r_curve[i], g_curve[i], b_curve[i])])
        if m != 0:
            for curve in (r_curve, g_curve, b_curve):
                curve[i] /= m


def rgb_contrast(r, g, b):
    '''
    Apply contrast correction on the colour curves using sRGB
    
    @param  r:float  The contrast parameter for the red curve
    @param  g:float  The contrast parameter for the green curve
    @param  b:float  The contrast parameter for the blue curve
    '''
    for (curve, level) in curves(r, g, b):
        if not level == 1.0:
            for i in range(i_size):
                curve[i] = (curve[i] - 0.5) * level + 0.5


def cie_contrast(level):
    '''
    Apply contrast correction on the colour curves using CIE XYZ
    
    @param  level:float  The brightness parameter
    '''
    if not level == 1.0:
        for i in range(i_size):
            (x, y, Y) = srgb_to_ciexyy(r_curve[i], g_curve[i], b_curve[i])
            (r_curve[i], g_curve[i], b_curve[i]) = to_rgb(x, y, (Y - 0.5) * level + 0.5)


def rgb_brightness(r, g, b):
    '''
    Apply brightness correction on the colour curves using sRGB
    
    @param  r:float  The brightness parameter for the red curve
    @param  g:float  The brightness parameter for the green curve
    @param  b:float  The brightness parameter for the blue curve
    '''
    for (curve, level) in curves(r, g, b):
        if not level == 1.0:
            for i in range(i_size):
                curve[i] *= level


def cie_brightness(level):
    '''
    Apply brightness correction on the colour curves using CIE XYZ
    
    @param  level:float  The brightness parameter
    '''
    if not level == 1.0:
        for i in range(i_size):
            (x, y, Y) = srgb_to_ciexyy(r_curve[i], g_curve[i], b_curve[i])
            (r_curve[i], g_curve[i], b_curve[i]) = to_rgb(x, y, Y * level)


def gamma(r, g, b):
    '''
    Apply gamma correction on the colour curves
    
    @param  r:float  The gamma parameter for the red curve
    @param  g:float  The gamma parameter for the green curve
    @param  b:float  The gamma parameter for the blue curve
    '''
    for (curve, level) in curves(r, g, b):
        if not level == 1.0:
            for i in range(i_size):
                curve[i] **= level

    
def sigmoid(r, g, b):
    '''
    Apply S-curve correction on the colour curves
    
    @param  r:float?  The sigmoid parameter for the red curve
    @param  g:float?  The sigmoid parameter for the green curve
    @param  b:float?  The sigmoid parameter for the blue curve
    '''
    for (curve, level) in curves(r, g, b):
        if level is not None:
            for i in range(i_size):
                try:
                    curve[i] = 0.5 - math.log(1 / curve[i] - 1) / level
                except:
                    curve[i] = 0;


def clip():
    '''
    Clip all values below the actual minimum and above actual maximums
    '''
    for curve in (r_curve, g_curve, b_curve):
        for i in range(i_size):
            curve[i] = min(max(0.0, curve[i]), 1.0)