| 1 | # /=====================================================================\ # | ||
| 2 | # | LaTeXML::Common::Color::hsb | # | ||
| 3 | # | A representation of colors in the hsb 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::hsb; | ||
| 13 | use strict; | ||
| 14 | use warnings; | ||
| 15 | use base qw(LaTeXML::Common::Color); | ||
| 16 | use LaTeXML::Global; | ||
| 17 | use LaTeXML::Common::Error; | ||
| 18 | |||
| 19 | sub hsb { my ($self) = @_; return $self; } | ||
| 20 | |||
| 21 | sub rgb { | ||
| 22 | my ($self) = @_; | ||
| 23 | my ($model, $h, $s, $b) = @$self; | ||
| 24 | my $i = int(6 * $h); | ||
| 25 | my $f = 6 * $h - $i; | ||
| 26 | my $u = $b * (1 - $s * (1 - $f)); | ||
| 27 | my $v = $b * (1 - $s * $f); | ||
| 28 | my $w = $b * (1 - $s); | ||
| 29 | if ($i == 0) { return LaTeXML::Common::Color->new('rgb', $b, $u, $w); } | ||
| 30 | elsif ($i == 1) { return LaTeXML::Common::Color->new('rgb', $v, $b, $w); } | ||
| 31 | elsif ($i == 2) { return LaTeXML::Common::Color->new('rgb', $w, $b, $u); } | ||
| 32 | elsif ($i == 3) { return LaTeXML::Common::Color->new('rgb', $w, $v, $b); } | ||
| 33 | elsif ($i == 4) { return LaTeXML::Common::Color->new('rgb', $u, $w, $b); } | ||
| 34 | elsif ($i == 5) { return LaTeXML::Common::Color->new('rgb', $b, $w, $v); } | ||
| 35 | elsif ($i == 6) { return LaTeXML::Common::Color->new('rgb', $b, $w, $w); } | ||
| 36 | else { # We need a fallback (e.g. pacify wild arXiv errors) | ||
| 37 | Error('misdefined', 'hsb', undef, "Expected h between 0 and 1 in conversion to rgb, got h $h"); | ||
| 38 | return LaTeXML::Common::Color->new('rgb', $b, $w, $w); } } | ||
| 39 | |||
| 40 | sub cmy { my ($self) = @_; return $self->rgb->cmy; } | ||
| 41 | sub cmyk { my ($self) = @_; return $self->rgb->cmyk; } | ||
| 42 | sub gray { my ($self) = @_; return $self->rgb->gray; } | ||
| 43 | |||
| 44 | sub complement { | ||
| 45 | my ($self) = @_; | ||
| 46 | my ($h, $s, $b) = $self->components; | ||
| 47 | my $hp = ($h < 0.5 ? $h + 0.5 : $h - 0.5); | ||
| 48 | my $bp = 1 - $b * (1 - $s); | ||
| 49 | my $sp = ($bp == 0 ? 0 : $b * $s / $bp); | ||
| 50 | return $self->new($hp, $sp, $bp); } | ||
| 51 | |||
| 52 | sub mix { | ||
| 53 | my ($self, $color, $fraction) = @_; | ||
| 54 | # I don't quite follow what Kern's saying, on a quick read, | ||
| 55 | # so we'll punt by doing the conversion in rgb space, then converting back. | ||
| 56 | return $self->rgb->mix($color, $fraction)->hsb; } | ||
| 57 | |||
| 58 | #====================================================================== | ||
| 59 | 1; | ||
| 60 | |||
| 61 | __END__ |