Created
July 9, 2012 07:02
-
-
Save oliverguenther/3074725 to your computer and use it in GitHub Desktop.
Perl snippet: Conversion from CIE 1931 XYZ color space to L* u* v*
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
# Reference X_n , Y_n , Z_n default to 1 | |
my ($Xn, $Yn, $Zn); | |
$Xn = $Yn = $Zn = 1; | |
# Input values XYZ (as input to file i.e.: "0.7771 0.9185 0.1796") | |
my $X = shift; | |
my $Y = shift; | |
my $Z = shift; | |
# Case threshold | |
my $thresh = ($Y/$Yn); | |
# Compute L* value | |
my $Ls; | |
if ($thresh > 0.008856) { | |
$Ls = 116 * (($Y/$Yn) ** (1/3)) - 16; | |
} else { | |
$Ls = 903.3 * ($Y/$Yn); | |
} | |
# Compute values U' and Un | |
my $u = (4*$X) / ($X + (15*$Y) + (3*$Z)); | |
my $un = (4*$Xn) / ($Xn + (15*$Yn) + (3*$Zn)); | |
# Compute values V' and Vn | |
my $v = (9*$Y) / ($X + (15*$Y) + (3*$Z)); | |
my $vn = (9*$Yn) / ($Xn + (15*$Yn) + (3*$Zn)); | |
# Compute u* | |
my $us = 13 * $Ls * ($u - $un); | |
# Compute v* | |
my $vs = 13 * $Ls * ($v - $vn); | |
print "Result:\nL* = $Ls\nu* = $us\nv* = $vs\n\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment