| 1 | # /=====================================================================\ # | ||||||||||||||||||||||||||||
| 2 | # | LaTeXML::Common::Color ,... | # | ||||||||||||||||||||||||||||
| 3 | # | Representation of colors in various color models | # | ||||||||||||||||||||||||||||
| 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; | ||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 5 | ▶ | use LaTeXML::Global; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 5 | ▶ | use LaTeXML::Common::Object; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 5 | ▶ | use LaTeXML::Common::Error; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 5 | ▶ | use base qw(LaTeXML::Common::Object); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 5 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 5 | our @EXPORT = ( # Global STATE; This gets bound by LaTeXML.pm | |||||||||||||||||||||||||||
| 21 | qw( &Color &Black &White), | ||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | #====================================================================== | ||||||||||||||||||||||||||||
| 25 | # Exported constructors | ||||||||||||||||||||||||||||
| 26 | 0 | ▶ | sub Color { | ||||||||||||||||||||||||||
|
Subroutine
Color: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 0 | my ($model, @components) = @_; | |||||||||||||||||||||||||||
| 28 | 0 | return LaTeXML::Common::Color->new(ToString($model), map { ToString($_) } @components); } | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 5 | ▶ | use constant Black => bless ['rgb', 0, 0, 0], 'LaTeXML::Common::Color::rgb'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 5 | ▶ | use constant White => bless ['rgb', 1, 1, 1], 'LaTeXML::Common::Color::rgb'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||||||||||||||||||||||||||||
| 34 | # Color objects; objects representing color in "arbitrary" color models | ||||||||||||||||||||||||||||
| 35 | # We'd like to provide a set of "core" color models (rgb,cmy,cmyk,hsb) | ||||||||||||||||||||||||||||
| 36 | # and allow derived color models (with scaled ranges, or whatever; see xcolor). | ||||||||||||||||||||||||||||
| 37 | # There is some awkwardness in that we'd like to support the core models | ||||||||||||||||||||||||||||
| 38 | # directly with built-in code, but support derived models that possibly | ||||||||||||||||||||||||||||
| 39 | # are defined in terms of macros defined as part of a style file. | ||||||||||||||||||||||||||||
| 40 | #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||||||||||||||||||||||||||||
| 41 | # NOTE: This class is in Common since it could conceivably be useful | ||||||||||||||||||||||||||||
| 42 | # in Postprocessing --- But the API, includes, etc haven't been tuned for that! | ||||||||||||||||||||||||||||
| 43 | # They only use $STATE to get derived color information, Error, min & max. | ||||||||||||||||||||||||||||
| 44 | #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||||||||||||||||||||||||||||
| 45 | # Color Objects | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 5 | our %core_color_models = map { ($_ => 1) } qw(rgb cmy cmyk hsb gray); # [CONSTANT] | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # slightly contrived to avoid 'use'ing all the models in here | ||||||||||||||||||||||||||||
| 50 | # (which causes compiler redefined issues, and preloads them all) | ||||||||||||||||||||||||||||
| 51 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 52 | 0 | my ($class, @components) = @_; | |||||||||||||||||||||||||||
| 53 | 0 | ▶ | if (ref $class) { # from $self->new(...) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | return bless [$$class[0], @components], ref $class; } | |||||||||||||||||||||||||||
| 55 | else { # Else, $model is the 1st element of @components; | ||||||||||||||||||||||||||||
| 56 | 0 | my $model = shift(@components); | |||||||||||||||||||||||||||
| 57 | 0 | ▶ | my $type = ($core_color_models{$model} ? $model : 'Derived'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | my $class = 'LaTeXML::Common::Color::' . $type; | |||||||||||||||||||||||||||
| 59 | 0 | ▶ | if (($type eq 'Derived') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$type eq "Derived" and not $LaTeXML::Common::Color::STATE->lookupValue('derived_color_model_' . $model)
MC/DC0%0/2
$type eq "Derived" and not $LaTeXML::Common::Color::STATE->lookupValue('derived_color_model_' . $model)
$type eq "Derived"
not $LaTeXML::Common::Color::STATE->lookupValue('derived_color_model_' . $model)
Truth table0%0/3
A and not(B)
A= $type eq "Derived"B= not $LaTeXML::Common::Color::STATE->lookupValue('derived_color_model_' . $model)
| |||||||||||||||||||||||||||||
| 60 | && !$STATE->lookupValue('derived_color_model_' . $model)) { | ||||||||||||||||||||||||||||
| 61 | 0 | Error('unexpected', $model, undef, "Unrecognized color model '$model'"); } | |||||||||||||||||||||||||||
| 62 | 0 | my $module = $class . '.pm'; | |||||||||||||||||||||||||||
| 63 | 0 | $module =~ s|::|/|g; | |||||||||||||||||||||||||||
| 64 | 0 | ▶ | require $module unless exists $INC{$module}; # Load if not already loaded | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | return bless [$model, @components], $class; } } | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | sub model { | ||||||||||||||||||||||||||
|
Subroutine
model: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 69 | 0 | return $$self[0]; } | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | sub components { | ||||||||||||||||||||||||||
|
Subroutine
components: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 73 | 0 | my ($m, @comp) = @$self; | |||||||||||||||||||||||||||
| 74 | 0 | return @comp; } | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # Convert a color to another model | ||||||||||||||||||||||||||||
| 77 | 0 | ▶ | sub convert { | ||||||||||||||||||||||||||
|
Subroutine
convert: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | 0 | my ($self, $tomodel) = @_; | |||||||||||||||||||||||||||
| 79 | 0 | ▶ | if ($self->model eq $tomodel) { # Already the correct model | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 80 | 0 | return $self; } | |||||||||||||||||||||||||||
| 81 | elsif ($core_color_models{$tomodel}) { # target must be core model | ||||||||||||||||||||||||||||
| 82 | 0 | return $self->toCore->$tomodel; } | |||||||||||||||||||||||||||
| 83 | elsif (my $data = $STATE->lookupValue('derived_color_model_' . $tomodel)) { # Ah, target is a derived color | ||||||||||||||||||||||||||||
| 84 | 0 | my $coremodel = $$data[0]; | |||||||||||||||||||||||||||
| 85 | 0 | my $convertfrom = $$data[2]; | |||||||||||||||||||||||||||
| 86 | 0 | return &{$convertfrom}($self->$coremodel); } | |||||||||||||||||||||||||||
| 87 | else { | ||||||||||||||||||||||||||||
| 88 | 0 | Error('unexpected', $tomodel, undef, "Unrecognized color model '$tomodel'"); | |||||||||||||||||||||||||||
| 89 | 0 | return $self; } } | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | sub toString { | ||||||||||||||||||||||||||
|
Subroutine
toString: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 92 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 93 | 0 | my ($model, @comp) = @$self; | |||||||||||||||||||||||||||
| 94 | 0 | return $model . "(" . join(',', @comp) . ")"; } | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | sub stringify { | ||||||||||||||||||||||||||
|
Subroutine
stringify: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 97 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 98 | 0 | my ($model, @comp) = @$self; | |||||||||||||||||||||||||||
| 99 | 0 | return "Color::" . $model . "(" . join(',', @comp) . ")"; } | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub toHex { | ||||||||||||||||||||||||||
|
Subroutine
toHex: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 102 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 103 | 0 | return $self->rgb->toHex; } | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | sub toAttribute { | ||||||||||||||||||||||||||
|
Subroutine
toAttribute: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 106 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 107 | 0 | return $self->rgb->toHex; } | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # Convert the color to a core model; Assume it already is! | ||||||||||||||||||||||||||||
| 110 | # Color::Derived MUST override this... | ||||||||||||||||||||||||||||
| 111 | 0 | ▶ | sub toCore { my ($self) = @_; return $self; } | ||||||||||||||||||||||||||
|
Subroutine
toCore: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | #====================================================================== | ||||||||||||||||||||||||||||
| 114 | # By default, just complement components (works for rgb, cmy, gray) | ||||||||||||||||||||||||||||
| 115 | 0 | ▶ | sub complement { | ||||||||||||||||||||||||||
|
Subroutine
complement: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 116 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 117 | 0 | return $self->new(map { 1 - $_ } $self->components); } | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # Mix $self*$fraction + $color*(1-$fraction) | ||||||||||||||||||||||||||||
| 120 | 0 | ▶ | sub mix { | ||||||||||||||||||||||||||
|
Subroutine
mix: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | 0 | my ($self, $color, $fraction) = @_; | |||||||||||||||||||||||||||
| 122 | 0 | ▶ | $color = $color->convert($self->model) unless $self->model eq $color->model; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | my @a = $self->components; | |||||||||||||||||||||||||||
| 124 | 0 | my @b = $color->components; | |||||||||||||||||||||||||||
| 125 | 0 | return $self->new(map { $fraction * $a[$_] + (1 - $fraction) * $b[$_] } 0 .. $#a); } | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | ▶ | sub add { | ||||||||||||||||||||||||||
|
Subroutine
add: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 128 | 0 | my ($self, $color) = @_; | |||||||||||||||||||||||||||
| 129 | 0 | ▶ | $color = $color->convert($self->model) unless $self->model eq $color->model; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 0 | my @a = $self->components; | |||||||||||||||||||||||||||
| 131 | 0 | my @b = $color->components; | |||||||||||||||||||||||||||
| 132 | 0 | return $self->new(map { $a[$_] + $b[$_] } 0 .. $#a); } | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | # The next 2 methods multiply the components of a color by some value(s) | ||||||||||||||||||||||||||||
| 135 | # This assumes that such a thing makes sense in the given model, for some purpose. | ||||||||||||||||||||||||||||
| 136 | # It may be that the components should be truncated to 1 (or some other max?) | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | # Multiply all components by a constant | ||||||||||||||||||||||||||||
| 139 | 0 | ▶ | sub scale { | ||||||||||||||||||||||||||
|
Subroutine
scale: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 140 | 0 | my ($self, $m) = @_; | |||||||||||||||||||||||||||
| 141 | 0 | return $self->new(map { $m * $_ } $self->components); } | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | # Multiply by a vector (must have same number of components) | ||||||||||||||||||||||||||||
| 144 | # This may or may not make sense for any given color model or purpose. | ||||||||||||||||||||||||||||
| 145 | 0 | ▶ | sub multiply { | ||||||||||||||||||||||||||
|
Subroutine
multiply: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 146 | 0 | my ($self, @m) = @_; | |||||||||||||||||||||||||||
| 147 | 0 | my @c = $self->components; | |||||||||||||||||||||||||||
| 148 | 0 | ▶ | if (scalar(@m) != scalar(@c)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 0 | Error('misdefined', 'multiply', "Multiplying color components by wrong number of parts", | |||||||||||||||||||||||||||
| 150 | "The color is " . ToString($self) . " while the multipliers are " . join(',', @m)); | ||||||||||||||||||||||||||||
| 151 | 0 | return $self; } | |||||||||||||||||||||||||||
| 152 | else { | ||||||||||||||||||||||||||||
| 153 | 0 | return $self->new(map { $c[$_] * $m[$_] } 0 .. $#c); } } | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||||||||||||||||||||||||||||
| 156 | 5 | 1; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | __END__ | ||||||||||||||||||||||||||||