| 1 | # -*- indent-tabs-mode: nil -*- | ||
| 2 | |||
| 3 | =head1 SEE ALSO | ||
| 4 | |||
| 5 | L<Graphics::ColorUtils> | ||
| 6 | |||
| 7 | L(<https://qiita.com/yoya/items/96c36b069e74398796f3> | ||
| 8 | |||
| 9 | =cut | ||
| 10 | |||
| 11 | package Term::ANSIColor::Concise::ColorUtils; | ||
| 12 | |||
| 13 | our $VERSION = "3.06"; | ||
| 14 | |||
| 15 | use v5.14; | ||
| 16 | use warnings; | ||
| 17 | use utf8; | ||
| 18 | |||
| 19 | use Data::Dumper; | ||
| 20 | use Graphics::ColorUtils; | ||
| 21 | use List::Util qw(min max pairs); | ||
| 22 | |||
| 23 | sub clip { max(min($_[0], $_[1]), 0) } | ||
| 24 | |||
| 25 | sub rgb { | ||
| 26 | my $self = shift; | ||
| 27 | my $class = ref $self || $self; | ||
| 28 | if (@_) { | ||
| 29 | bless [ map { max(min(255, $_), 0) } @_ ], $class; | ||
| 30 | } else { | ||
| 31 | map int, @{$self}; | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | sub hsl2rgb { hls2rgb $_[0], $_[2], $_[1] } | ||
| 36 | sub rgb2hsl { (rgb2hls(@_))[0,2,1] } | ||
| 37 | |||
| 38 | sub hsl { | ||
| 39 | my $self = shift; | ||
| 40 | my $class = ref $self || $self; | ||
| 41 | if (@_) { | ||
| 42 | my($h, $s, $l) = @_; | ||
| 43 | $class->rgb(hls2rgb($h, $l/100, $s/100)); | ||
| 44 | } else { | ||
| 45 | my @hsl = rgb2hsl($self->rgb); | ||
| 46 | map int, ($hsl[0], $hsl[1] * 100, $hsl[2] * 100); | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | sub yiq { | ||
| 51 | my $self = shift; | ||
| 52 | my $class = ref $self || $self; | ||
| 53 | if (@_) { | ||
| 54 | my($y, $i, $q) = @_; | ||
| 55 | $class->rgb(yiq2rgb($y/100, $i/100, $q/100)); | ||
| 56 | } else { | ||
| 57 | my($y, $i, $q) = rgb2yiq($self->rgb); | ||
| 58 | map int, ($y*100, $i*100, $q*100); | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | sub luminance { | ||
| 63 | my $self = shift; | ||
| 64 | my $class = ref $self || $self; | ||
| 65 | if (@_) { | ||
| 66 | my($y, $i, $q) = $self->yiq; | ||
| 67 | my @yiq = $self->yiq; | ||
| 68 | $class->yiq($_[0], $yiq[1], $yiq[2]); | ||
| 69 | } else { | ||
| 70 | ($self->yiq)[0]; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | 1; |