Skip to content

Instantly share code, notes, and snippets.

@Jared-Prime
Created June 9, 2012 03:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jared-Prime/2899323 to your computer and use it in GitHub Desktop.
Save Jared-Prime/2899323 to your computer and use it in GitHub Desktop.
conversions from L*a*b to RGB
# port of David Dalrymple's GNU C code
# the original C code can be found at http://davidad.net/colorviz/
def lab_to_xyz(lab)
l = lab[0]
a = lab[1]
b = lab[2]
ill = [0.9643, 1.0, 0.8251]
sl = (l + 0.16) / 1.16
x = ill[0] * finv(sl + (a/5.0))
y = ill[1] * finv(sl)
z = ill[2] * finv(sl = (b/2.0))
return x, y, z
end
def finv(t)
ratio = (6.0/29.0)
t > ratio ? (t*t*t) : (3.0*ratio*ratio*(t - (4.0/29.0)))
end
def xyz_to_rgb(xyz)
x = lab[0]
y = lab[1]
z = lab[2]
rl = (3.2406*x) - (1.5372*y) - (0.4986*z)
gl = (-0.9689*x) + (1.8758*y) + (0.0415*z)
bl = (0.0557*x ) - (0.2040*y) + (1.0570*z)
clip = (rl<0.0 || rl>1.0 || gl<0.0 || gl>1.0 || bl<0.0 || bl>1.0)
if clip
rl = (rl<0.0) ? 0.0 : ((rl>1.0) ? 1.0 : rl )
gl = (gl<0.0) ? 0.0 : ((gl>1.0) ? 1.0 : gl )
bl = (bl<0.0) ? 0.0 : ((bl>1.0) ? 1.0 : bl )
end
r = 255.0 * correct(rl)
g = 255.0 * correct(gl)
b = 255.0 * correct(bl)
return r, g, b
end
def correct(c)
a = 0.055
return (c<=0.0031308) ? (12.92*c) : ((1 + a) * (c**(1/2.4)) - a)
end
def lab_to_rgb(lab)
lab_to_xyz(lab)
xyz_to_rgb(xyz)
end
def cl_to_pix(rgb, quant, qual)
tau = 2*Math::PI
ptr = rgb
l = qual*0.61 + 0.09
angle = (tau/6.0) - (quant*tau)
r = qual * 0.311 + 0.125
a = Math.sin(angle) * r
b = Math.cos(angle) * r
lab_to_rgb([l,a,b])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment