#!/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 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
from curve import *
def translate_to_integers():
'''
Translate the curves from float to integer
@param :(r:list, g:list, b:list) The red curve, the green curve and,
the blue curve mapped to integers
'''
R_curve, G_curve, B_curve = [0] * i_size, [0] * i_size, [0] * i_size
for i_curve, o_curve in ((r_curve, R_curve), (g_curve, G_curve), (b_curve, B_curve)):
for i in range(i_size):
o_curve[i] = int(i_curve[i] * (o_size - 1) + 0.5)
if clip_result:
o_curve[i] = min(max(0, o_curve[i]), (o_size - 1))
return (R_curve, G_curve, B_curve)
def ramps_to_function(r, g, b):
'''
Convert a three colour curves to a function that applies those adjustments
@param r:list The red colour curves as [0, 65535] integers
@param g:list The green colour curves as [0, 65535] integers
@param b:list The blue colour curves as [0, 65535] integers
@return :()→void Function to invoke to apply the curves that the parameters [r, g and b] represents
'''
r = [y / 65535 for y in r]
g = [y / 65535 for y in g]
b = [y / 65535 for y in b]
return functionise((r, g, b))
def linearly_interpolate_ramp(r, g, b): # TODO test, demo and document this
'''
Linearly interpolate ramps to the size of the output axes
@param r:list The red colour curves
@param g:list The green colour curves
@param b:list The blue colour curves
@return :(r:list, g:list, b:list) The input parameters extended to sizes of `o_size`,
or their original size, whatever is larger.
'''
R, G, B = None, None, None
R = r[:] if len(r) >= o_size else ([None] * o_size)
G = g[:] if len(g) >= o_size else ([None] * o_size)
B = b[:] if len(b) >= o_size else ([None] * o_size)
for small, large in curves(R, G, B):
if len(large) > len(small):
small_ = len(small) - 1
large_ = len(large) - 1
for i in range(len(large)):
j = i * small_ / large_
j, w = int(j), j % 1
k = j + 1
if k > small_:
k = small_
large[i] = small[j] * (w - 1) + small[k] * w
return (R, G, B)
def functionise(rgb):
'''
Convert a three colour curves to a function that applies those adjustments
@param rgb:(r:list, g:list, b:list) The colour curves as [0, 1] values
@return :()→void Function to invoke to apply the curves
that the parameters [r, g and b] represents
'''
def fcurve(R_curve, G_curve, B_curve):
for curve, cur in curves(R_curve, G_curve, B_curve):
for i in range(i_size):
y = int(curve[i] * (len(cur) - 1) + 0.5)
y = min(max(0, y), len(cur) - 1)
curve[i] = cur[y]
return lambda : fcurve(*rgb)
def store():
'''
Store the current adjustments
@return :(r:list, g:list, b:list) The colour curves
'''
return (r_curve[:], g_curve[:], b_curve[:])
def restore(rgb):
'''
Discard any currently applied adjustments and apply stored adjustments
@param rgb:(r:list, g:list, b:list) The colour curves to restore
'''
(r_curve[:], g_curve[:], b_curve[:]) = rgb