Skip to content

Instantly share code, notes, and snippets.

@kiyotakagoto
Created December 8, 2011 14:10
Show Gist options
  • Save kiyotakagoto/1447087 to your computer and use it in GitHub Desktop.
Save kiyotakagoto/1447087 to your computer and use it in GitHub Desktop.
sRGBをXYZ表色系に変換(perl)
# 参考:http://www005.upp.so-net.ne.jp/fumoto/linkp25.htm
package sRGBtoXYZ;
use strict;
use warnings;
sub srgb_to_xyz {
my $rgb_href = shift;
my $rgb_dash_href = {
Rdash => $rgb_href->{R} / 255,
Gdash => $rgb_href->{G} / 255,
Bdash => $rgb_href->{B} / 255,
};
# to liner RGB
my $liner_rgb_href = {
linerR => $rgb_dash_href->{Rdash} ** 2.2,
linerG => $rgb_dash_href->{Gdash} ** 2.2,
linerB => $rgb_dash_href->{Bdash} ** 2.2,
};
# calc X, Y, Z
my $xyz = {
X => (0.3933 * $liner_rgb_href->{linerR} + 0.3651 * $liner_rgb_href->{linerG} + 0.1903 * $liner_rgb_href->{linerB}) * 100,
Y => (0.2123 * $liner_rgb_href->{linerR} + 0.7010 * $liner_rgb_href->{linerG} + 0.0858 * $liner_rgb_href->{linerB}) * 100,
Z => (0.0182 * $liner_rgb_href->{linerR} + 0.1117 * $liner_rgb_href->{linerG} + 0.9570 * $liner_rgb_href->{linerB}) * 100,
};
return $xyz;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment