From bd6427412a85741be7ef7846bae228c79cbfcb22 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Thu, 13 Feb 2014 03:56:43 +0100 Subject: colour temperature MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/__main__.py | 219 ++++++++++++++++++++++++++++++++++++++++++++++++-------- src/colour.py | 56 +++++++++++++++ 2 files changed, 246 insertions(+), 29 deletions(-) create mode 100644 src/colour.py (limited to 'src') diff --git a/src/__main__.py b/src/__main__.py index 722f879..92b8cdc 100755 --- a/src/__main__.py +++ b/src/__main__.py @@ -16,10 +16,20 @@ # along with this program. If not, see . import math +from colour import * -r_curve = [i / 255 for i in range(256)] -g_curve = [i / 255 for i in range(256)] -b_curve = [i / 255 for i in range(256)] + +DATADIR = '.' + +i_size = 2 ** 8 +o_size = 2 ** 16 +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 + +cmf_2deg_cache = None +cmf_10deg_cache = None def curves(r, g, b): @@ -34,26 +44,132 @@ def curves(r, g, b): return ((r_curve, r), (g_curve, g), (b_curve, b)) -def sigmoid(r, g, b): +def series_d(temperature): ''' - Apply S-curve correction on the colour curves + Calculate the colour for a blackbody temperature - @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 + @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 ''' - for (curve, level) in curves(r, g, b): - if not level == 1.0: - for i in range(256): - try: - curve[i] = 0.5 - math.log(1 / curve[i] - 1) / level - except: - curve[i] = 0; + 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 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) + +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 to_srgb(x, y, 1.0) + +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 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 contrast(r, g, b): +def rgb_contrast(r, g, b): ''' - Apply contrast correction on the colour curves + 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 @@ -61,12 +177,23 @@ def contrast(r, g, b): ''' for (curve, level) in curves(r, g, b): if not level == 1.0: - for i in range(256): + for i in range(i_size): curve[i] = (curve[i] - 0.5) * level + 0.5 -def brightness(r, g, b): +def cie_contrast(level): ''' - Apply brightness correction on the colour curves + 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) = 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 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 @@ -74,9 +201,20 @@ def brightness(r, g, b): ''' for (curve, level) in curves(r, g, b): if not level == 1.0: - for i in range(256): + 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) = 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 @@ -87,31 +225,54 @@ def gamma(r, g, b): ''' for (curve, level) in curves(r, g, b): if not level == 1.0: - for i in range(256): + 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 belowed the actual minimum and above actual maximums ''' for curve in (r_curve, g_curve, b_curve): - for i in range(256): + for i in range(i_size): curve[i] = min(max(0.0, curve[i]), 1.0) -sigmoid(1.0, 1.0, 1.0) +temperature(6500, series_d, True) +divide_by_maximum() +temperature(6500, simple_whitepoint, True) clip() -contrast(1.0, 1.0, 1.0) -brightness(1.0, 1.0, 1.0) +rgb_contrast(1.0, 1.0, 1.0) +cie_contrast(1.0) +rgb_brightness(1.0, 1.0, 1.0) +cie_brightness(1.0) gamma(1.0, 1.0, 1.0) +sigmoid(None, None, None) clip() for curve in (r_curve, g_curve, b_curve): - for i in range(256): - curve[i] = int(curve[i] * 65535 + 0.5) + for i in range(i_size): + curve[i] = int(curve[i] * (o_size - 1) + 0.5) + if clip_result: + curve[i] = min(max(0, curve[i]), (o_size - 1)) print(r_curve) print(g_curve) print(b_curve) -print(Math.e) + diff --git a/src/colour.py b/src/colour.py new file mode 100644 index 0000000..df5b38d --- /dev/null +++ b/src/colour.py @@ -0,0 +1,56 @@ +#!/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 . + + +def linear_to_standard(*colour): + 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): + 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): + return [Y * x / y, Y, Y * (1 - x - y) / y] + +def ciexyz_to_ciexyy(X, Y, Z): + p = -Y / X + q = 1 + Y / X + y = 1 / (p / 2 + (p ** 2 / 4 - q) ** 0.5) + x = X * y / Y + return [x, y, Y] + +def ciexyz_to_linear(X, Y, Z): + r = 3.2406 * X - 1.5372 * Y - 0.4986 * Z + g = -0.9689 * X + 1.8758 * Y + 0.0415 * Z + b = 0.0557 * X - 0.2040 * Y + 1.0570 * Z + return [r, g, b] + +def linear_to_ciexyz(r, g, b): + X = 0.4124 * r + 0.3576 * g + 0.1805 * b + Y = 0.2126 * r + 0.7152 * g + 0.0722 * b + Z = 0.0193 * r + 0.1192 * g + 1.9502 * b + return [X, Y, Z] + +def to_ciexyy(r, g, b): + (r, g, b) = standard_to_linear(r, g, b) + (X, Y, Z) = linear_to_ciexyz(r, g, b) + return ciexyz_to_ciexyy(X, Y, Z) + +def to_srgb(x, y, Y): + (X, Y, Z) = ciexyy_to_ciexyz(x, y, Y) + (r, g, b) = ciexyz_to_linear(X, Y, Z) + return linear_to_standard(r, g, b) + -- cgit v1.2.3-70-g09d2