lib/LaTeXML/Common/Color/rgb.pm untested

filter: Statementstmt 0.0% 0 / 42 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 76 CC 8 SCAR 34.5
CC
8
Cov
0%
hsb
72.0
rgb
2.0
cmy
2.0
CRAP 31.5
1 # /=====================================================================\ #
2 # | LaTeXML::Common::Color::rgb | #
3 # | A representation of colors in the rgb color model | #
4 # |=====================================================================| #
5 # | Part of LaTeXML: | #
6 # | Public domain software, produced as part of work done by the | #
7 # | United States Government & not subject to copyright in the US. | #
8 # |---------------------------------------------------------------------| #
9 # | Bruce Miller <bruce.miller@nist.gov> #_# | #
10 # | http://dlmf.nist.gov/LaTeXML/ (o o) | #
11 # \=========================================================ooo==U==ooo=/ #
12 package LaTeXML::Common::Color::rgb;
13 use strict;
14 use warnings;
15 use base qw(LaTeXML::Common::Color);
16 use LaTeXML::Global;
17  
18 sub rgb {
19 my ($self) = @_;
20 return $self; }
21  
22 # Convert to cmy, cmyk,
23 sub cmy {
24 my ($self) = @_;
25 return LaTeXML::Common::Color->new('cmy', 1 - $$self[1], 1 - $$self[2], 1 - $$self[3]); }
26  
27 sub cmyk {
28 my ($self) = @_;
29 return $self->cmy->cmyk; }
30  
31 sub gray {
32 my ($self) = @_;
33 return LaTeXML::Common::Color->new('gray', 0.3 * $$self[1] + 0.59 * $$self[2] + 0.11 * $$self[3]); }
34  
35 # See Section 6.3.1 in xcolor documentation Dr.Uwe Kern; xcolor.pdf
36 sub Phi {
37 my ($x, $y, $z, $u, $v) = @_;
38 return LaTeXML::Common::Color->new('hsb', ($u * ($x - $z) + $v * ($x - $y)) / (6 * ($x - $z)),
39 ($x - $z) / $x,
40 $x); }
41  
42 sub hsb {
43 my ($self) = @_;
44 my ($m, $r, $g, $b) = @$self;
45 my $i = 4 * ($r >= $g) + 2 * ($g >= $b) + ($b >= $r);
46 if ($i == 1) { return Phi($b, $g, $r, 3, 1); }
47 elsif ($i == 2) { return Phi($g, $r, $b, 1, 1); }
48 elsif ($i == 3) { return Phi($g, $b, $r, 3, -1); }
49 elsif ($i == 4) { return Phi($r, $b, $g, 5, 1); }
50 elsif ($i == 5) { return Phi($b, $r, $g, 5, -1); }
51 elsif ($i == 6) { return Phi($r, $g, $b, 1, -1); }
52 elsif ($i == 7) { return LaTeXML::Common::Color->new('hsb', 0, 0, $b); } }
53  
54 sub hex2 {
55 my ($n) = @_;
56 my $nn = LaTeXML::Common::Number::roundto($n * 255, 0);
57 my $h_full = sprintf("%.2X", $nn);
58 # the precision doesn't quite get enforced on larger values,
59 # so let's try to substring explicitly
60 my $h2 = substr($h_full, 0, 2);
61 return $h2; }
62  
63 sub toHex {
64 my ($self) = @_;
65 my ($model, $r, $g, $b) = @$self;
66 return '#' . hex2($r) . hex2($g) . hex2($b); }
67  
68 #======================================================================
69 1;
70  
71 __END__