Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Gamut mapped LCH to RGB transform
"""
lch2rgb_gamutmapped.py
Gamut mapped LCH to RGB transform
Copyright (c) 2014 Jack Doerner
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
import numpy
import skimage.color
import scipy.optimize
import math
def lch2rgb_csolve(L, C, H, r_target=None, g_target=None, b_target=None, epsilon=0.0001):
# Here we use numpy's nonlinear equation solver to find a color with the same L and H
# values, but C scaled until the target r, g, or b is met. The color must first be
# converted to lab. In the future it may make sense for this function to call a separate
# lab conversion function.
# note: this method is very slow. A closed-form solution would be much better,
# but I haven't come up with one yet.
A = math.cos(H)
B = math.sin(H)
def equation(delta):
lab = numpy.asarray([L, A*delta, B*delta]).reshape((1,1,3))
(r, g, b) = skimage.color.lab2rgb(lab).reshape((3,))
result = 0
if (not (r_target is None)):
result += ((r - r_target) * 100) **2
if (not (g_target is None)):
result += ((g - g_target) * 100) **2
if (not (b_target is None)):
result += ((b - b_target) * 100) **2
return result
return scipy.optimize.root(equation, C, method='hybr', tol=epsilon)
def lch2rgb_relativeColorimetric(lch, epsilon=0.0001):
# Call this method with a single pixel's lch color value to find the closest in-gamut color
# Conversion uses a method similar to the 'relative colorimetric' rendering intent used in
# converting between RGB colorspaces. That is, Luminance and Hue are preserved, but Chroma
# (saturation) is reduced until a valid value is found.
# Input pixels should be numpy arrays with the shape (1,1,3) or (3,).
lch = lch.copy()
lch = lch.reshape((1,1,3))
lch[0,0,0] = max(min(100, lch[0,0,0]),0)
rgb = skimage.color.lab2rgb(skimage.color.lch2lab(lch))
if (rgb[0,0,0] > 1):
lch[0,0,1] = lch2rgb_csolve(lch[0,0,0], lch[0,0,1], lch[0,0,2], r_target = 1, epsilon = epsilon).x - epsilon
rgb = skimage.color.lab2rgb(skimage.color.lch2lab(lch))
elif (rgb[0,0,0] < 0):
lch[0,0,1] = lch2rgb_csolve(lch[0,0,0], lch[0,0,1], lch[0,0,2], r_target = 0, epsilon = epsilon).x - epsilon
rgb = skimage.color.lab2rgb(skimage.color.lch2lab(lch))
if (rgb[0,0,1] > 1):
lch[0,0,1] = lch2rgb_csolve(lch[0,0,0], lch[0,0,1], lch[0,0,2], g_target = 1, epsilon = epsilon).x - epsilon
rgb = skimage.color.lab2rgb(skimage.color.lch2lab(lch))
elif (rgb[0,0,1] < 0):
lch[0,0,1] = lch2rgb_csolve(lch[0,0,0], lch[0,0,1], lch[0,0,2], g_target = 0, epsilon = epsilon).x - epsilon
rgb = skimage.color.lab2rgb(skimage.color.lch2lab(lch))
if (rgb[0,0,2] > 1):
lch[0,0,1] = lch2rgb_csolve(lch[0,0,0], lch[0,0,1], lch[0,0,2], b_target = 1, epsilon = epsilon).x - epsilon
rgb = skimage.color.lab2rgb(skimage.color.lch2lab(lch))
elif (rgb[0,0,2] < 0):
lch[0,0,1] = lch2rgb_csolve(lch[0,0,0], lch[0,0,1], lch[0,0,2], b_target = 0, epsilon = epsilon).x - epsilon
rgb = skimage.color.lab2rgb(skimage.color.lch2lab(lch))
return rgb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment