/usr/share/perl5/Circos/LCH.pm is in circos 0.69.6+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | package Circos::LCH;
=pod
=head1 NAME
Circos::LCH - LCH (ab) to RGB conversion
=head1 SYNOPSIS
This module is not meant to be used directly.
=head1 DESCRIPTION
Circos is an application for the generation of publication-quality,
circularly composited renditions of genomic data and related
annotations.
Circos is particularly suited for visualizing alignments, conservation
and intra and inter-chromosomal relationships. However, Circos can be
used to plot any kind of 2D data in a circular layout - its use is not
limited to genomics. Circos' use of lines to relate position pairs
(ribbons add a thickness parameter to each end) is effective to
display relationships between objects or positions on one or more
scales.
All documentation is in the form of tutorials at L<http://www.circos.ca>.
=cut
# -------------------------------------------------------------------
use strict;
use warnings;
use base 'Exporter';
our @EXPORT = qw(
deltae
rgb_to_lch
lch_to_rgb
);
use Carp qw( carp confess croak );
use Digest::MD5 qw(md5_hex);
use Math::Round;
use Math::VecStat qw(sum min max);
use Math::Trig;
use lib "$FindBin::RealBin";
use lib "$FindBin::RealBin/../lib";
use lib "$FindBin::RealBin/lib";
use POSIX qw(pow);
use Circos::Constants;
use Circos::Debug;
use Circos::Error;
use Circos::Utils;
use Circos::RGB;
my $white = { 'D65' => [ 0.312713, 0.329016 ] }; # Daylight 6504K
my $srgb = {
white_point => 'D65',
gamma => 'sRGB', # 2.4,
m => [ [ 0.4124237575757575, 0.2126560000000000, 0.0193323636363636 ], [ 0.3575789999999999, 0.7151579999999998, 0.1191930000000000 ], [ 0.1804650000000000, 0.0721860000000000, 0.9504490000000001 ] ],
mstar => [ [ 3.2407109439941704, -0.9692581090654827, 0.0556349466243886 ], [ -1.5372603195869781, 1.8759955135292130, -0.2039948042894247 ], [ -0.4985709144606416, 0.0415556779089489, 1.0570639858633826 ] ],
};
sub deltae {
my ($rgb1,$rgb2) = @_;
my $lab1 = RGB_to_Lab([ map { $_/255 } @$rgb1 ]);
my $lab2 = RGB_to_Lab([ map { $_/255 } @$rgb2 ]);
return sqrt( sum ( map { ($lab1->[$_]-$lab2->[$_])**2 } (0..2) ) );
}
################################################################
# LCH to RGB
sub rgb_to_lch {
my ($r,$g,$b,$alpha) = @_;
my $lab = RGB_to_Lab([$r/255,$g/255,$b/255]);
my $lch = Lab_to_LCHab($lab);
push @$lch,$alpha if defined $alpha;
return @$lch;
}
sub lch_to_rgb {
my ($L,$C,$H,$alpha) = @_;
my $lab = LCHab_to_Lab([$L,$C,$H]);
my $rgb = Lab_to_RGB($lab);
push @$rgb, $alpha if defined $alpha;
return rgb_to_rgb255($rgb);
}
sub xyY_to_XYZ {
my ($xyy) = @_;
my ($x, $y, $Y) = @{$xyy};
my ($X, $Z);
if (! ($y == 0)) {
$X = $x * $Y / $y;
$Z = (1 - $x - $y) * $Y / $y;
} else {
$X = 0; $Y = 0; $Z = 0;
}
return [ $X, $Y, $Z ];
}
################################################################
# LCH to Lab
sub LCHab_to_Lab {
my $lch = shift;
my ($L, $C, $H) = @{$lch};
my ($a, $b);
$H *= $TWOPI/360;
my $th = tan($H);
$a = $C / sqrt( $th * $th + 1 );
$b = sqrt($C*$C - $a*$a);
#$H = $H - 2*pi*int($H / 2*pi); # convert H to 0..2*pi - this seems to be wrong
if ($H < 0) { $H = $H + 2*$PI; }
if ($H > $PI_HALF && $H < 3*$PI_HALF) { $a = - $a; }
if ($H > $PI) { $b = - $b; }
return [ $L, $a, $b ];
}
################################################################
# Lab to RGB
sub Lab_to_RGB {
my $lab = shift;
my $xyz_white = &RGB_to_XYZ([ 1.0, 1.0, 1.0 ], "sRGB");
my $xyz = &Lab_to_XYZ($lab, $xyz_white);
return &XYZ_to_RGB($xyz, "sRGB");
}
sub RGB_to_XYZ {
my $rgb = shift;
my $rgb_lin = &RGB_to_linear_RGB($rgb,$srgb);
my $xyz = &_mult_v3_m33($rgb_lin, $srgb->{m});
return ($xyz);
}
sub XYZ_to_RGB {
my ($xyz, $space) = @_;
my $rgb_lin = &_mult_v3_m33($xyz, $srgb->{mstar});
my $rgb = &linear_RGB_to_RGB($rgb_lin, $space);
return ($rgb);
}
sub linear_RGB_to_RGB {
my $rgb = shift;
my ($R, $G, $B) = @{$rgb};
my $s = $srgb;
if ($s->{gamma} eq 'sRGB') {
# handle special sRGB gamma curve
if ( abs($R) <= 0.0031308 ) { $R = 12.92 * $R; }
else { $R = 1.055 * &_apow($R, 1/2.4) - 0.055; };
if ( abs($G) <= 0.0031308 ) { $G = 12.92 * $G; }
else { $G = 1.055 * &_apow($G, 1/2.4) - 0.055; }
if ( abs($B) <= 0.0031308 ) { $B = 12.92 * $B; }
else { $B = 1.055 * &_apow($B, 1/2.4) - 0.055; }
} else {
$R = &_apow($R, 1/$s->{gamma});
$G = &_apow($G, 1/$s->{gamma});
$B = &_apow($B, 1/$s->{gamma});
}
return [ $R, $G, $B ];
}
sub RGB_to_linear_RGB {
my $rgb = shift;
my ($R, $G, $B) = @{$rgb};
my $s = $srgb;
if ($s->{gamma} eq 'sRGB') # handle special sRGB gamma curve
{
if ( abs($R) <= 0.04045 ) { $R = $R / 12.92; }
else { $R = &_apow( ( $R + 0.055 ) / 1.055 , 2.4 ); }
if ( abs($G) <= 0.04045 ) { $G = $G / 12.92; }
else { $G = &_apow( ( $G + 0.055 ) / 1.055 , 2.4 ); }
if ( abs($B) <= 0.04045 ) { $B = $B / 12.92; }
else { $B = &_apow( ( $B + 0.055 ) / 1.055 , 2.4 ); }
} else {
$R = &_apow($R, $s->{gamma});
$G = &_apow($G, $s->{gamma});
$B = &_apow($B, $s->{gamma});
}
return [ $R, $G, $B ];
}
sub RGB_to_Lab
{
my $rgb = shift;
my $xyz_white = &RGB_to_XYZ([ 1.0, 1.0, 1.0 ], $srgb);
my $xyz = &RGB_to_XYZ($rgb, $srgb);
return &XYZ_to_Lab($xyz, $xyz_white);
}
sub Lab_to_LCHab
{
my $lab = shift;
my ($L, $a, $b) = @{$lab};
my ($C, $H);
$C = sqrt( $a*$a + $b*$b );
$H = atan2( $b, $a );
$H = rad2deg($H);
return [ $L, $C, $H ];
}
sub XYZ_to_Lab
{
my ($xyz, $xyz_white) = @_;
my ($X, $Y, $Z) = @{$xyz};
my ($Xw, $Yw, $Zw) = @{$xyz_white};
my ($L, $a, $b);
my $epsilon = 0.008856;
my $kappa = 903.3;
my ($fx, $fy, $fz);
my ($xr, $yr, $zr) = ( $X / $Xw,
$Y / $Yw,
$Z / $Zw );
if ($xr > $epsilon) { $fx = pow($xr, 1/3); } else { $fx = ($kappa*$xr + 16)/116; }
if ($yr > $epsilon) { $fy = pow($yr, 1/3); } else { $fy = ($kappa*$yr + 16)/116; }
if ($zr > $epsilon) { $fz = pow($zr, 1/3); } else { $fz = ($kappa*$zr + 16)/116; }
$L = 116 * $fy - 16;
$a = 500 * ($fx - $fy);
$b = 200 * ($fy - $fz);
return [ $L, $a, $b ];
}
sub Lab_to_XYZ
{
my ($lab, $xyz_white) = @_;
my ($L, $a, $b) = @{$lab};
my ($Xw, $Yw, $Zw) = @{$xyz_white};
my ($X, $Y, $Z);
my $epsilon = 0.008856;
my $kappa = 903.3;
my ($fx, $fy, $fz);
my ($xr, $yr, $zr);
if ($L > $kappa*$epsilon) { $yr = pow( ($L + 16)/116, 3 ); } else { $yr = $L / $kappa; }
if ( $yr > $epsilon ) { $fy = ($L + 16)/116; } else { $fy = ($kappa*$yr + 16)/116; }
$fx = ($a / 500) + $fy;
$fz = $fy - ($b / 200);
if (pow($fx, 3) > $epsilon) { $xr = pow($fx, 3); } else { $xr = (116 * $fx - 16)/$kappa; }
if (pow($fz, 3) > $epsilon) { $zr = pow($fz, 3); } else { $zr = (116 * $fz - 16)/$kappa; }
if ($L > $kappa*$epsilon) { $yr = pow(($L + 16)/116, 3); } else { $yr = $L/$kappa; }
$X = $xr * $Xw;
$Y = $yr * $Yw;
$Z = $zr * $Zw;
return [ $X, $Y, $Z ];
}
sub _mult_v3_m33
{
my ($v, $m) = @_;
my $vout = [
( $v->[0] * $m->[0]->[0] + $v->[1] * $m->[1]->[0] + $v->[2] * $m->[2]->[0] ),
( $v->[0] * $m->[0]->[1] + $v->[1] * $m->[1]->[1] + $v->[2] * $m->[2]->[1] ),
( $v->[0] * $m->[0]->[2] + $v->[1] * $m->[1]->[2] + $v->[2] * $m->[2]->[2] )
];
return $vout;
}
sub _apow {
my ($v, $p) = @_;
return ($v >= 0 ?
pow($v, $p) :
-pow(-$v, $p));
}
1;
|