blib/lib/Term/Fabulous/Chart/Palette.pm

filter: Statementstmt 90.0% 18 / 20 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.5% 25 / 27 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Term/Fabulous/Widget/Text.pm ↑ Summary blib/lib/Term/Fabulous/Check.pm »
1 package Term::Fabulous::Chart::Palette;
2  
3 3 ▶use v5.32;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 our $VERSION = '0.05';
9  
10 3 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 our @EXPORT_OK = qw(
12 palette_colors palette_names is_palette_name chart_color mix_rgb luminance is_light_rgb
13 contrast_rgb ink_colors rgb_hex
14 );
15  
16 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 0 ▶use Term::Fabulous::Check qw(cell_color one_of);
Subroutine BEGIN: called CC=1 SCAR=2.6
18  
19 # The categorical palettes, each with steps for dark and for light
20 # backgrounds. "default" is the validated palette of the documentation:
21 # its order keeps neighboring series apart for readers with color vision
22 # deficiencies. The others are for looks.
23 my %PALETTES = (
24 default => {
25 dark => [qw(3987e5 d95926 199e70 c98500 d55181 008300 9085e9 e66767)],
26 light => [qw(2a78d6 eb6834 1baf7a eda100 e87ba4 008300 4a3aa7 e34948)],
27 },
28 classic => {
29 dark => [qw(61afef e06c75 98c379 e5c07b c678dd 56b6c2 d19a66 be5046)],
30 light => [qw(4078f2 e45649 50a14f c18401 a626a4 0184bc 986801 ca1243)],
31 },
32 pastel => {
33 dark => [qw(a1c9f4 ffb482 8de5a1 ff9f9b d0bbff debb9b fab0e4 b9f2f0)],
34 light => [qw(6c9bd2 e08b52 5cb871 dd6c66 a08ad8 b38b65 d27ab8 5fb8b4)],
35 },
36 vivid => {
37 dark => [qw(00a8ff ff5c8a 2ecc71 ffc300 b76cff 00d2c6 ff8a3d a3e635)],
38 light => [qw(0077cc d6336c 1e9e57 c79100 8a3ffc 00897b e8590c 6a9e00)],
39 },
40 );
41  
42 # The ink of text on a dark and on a light background; everything else
43 # (labels, grid, axes) is mixed from it and the background.
44 my %INK = ( dark => 0xFFFFFF, light => 0x0B0B0B );
45  
46 sub palette_names () {
47 my @names = sort keys %PALETTES;
48 return @names;
49 }
50  
51 sub is_palette_name ($name) {
52 return defined $name && !ref $name && exists $PALETTES{$name} ? 1 : 0;
53 }
54  
55 # The colors of a named palette as packed 0xRRGGBB integers, in their order.
56 sub palette_colors ( $name, $mode ) {
57 croak "Term::Fabulous::Chart::Palette: unknown palette '" . ( $name // 'undef' ) . "' (known: " . join( ', ', palette_names() ) . ")"
58 unless is_palette_name($name);
59 one_of( __PACKAGE__, mode => $mode, keys %INK );
60 return map { hex } $PALETTES{$name}{$mode}->@*;
61 }
62  
63 # A user's color as ( 0xRRGGBB, opacity from 0 to 1 ): every format a
64 # canvas cell takes. Dies like the other color checks.
65 sub chart_color ( $owner, $name, $value ) {
66 my ( $red, $green, $blue, $alpha ) = cell_color( $owner, $name, $value )->@*;
67 return ( ( $red << 16 ) | ( $green << 8 ) | $blue, $alpha / 255 );
68 }
69  
70 # $from mixed with $amount (0 to 1) of $to.
71 sub mix_rgb ( $from, $to, $amount ) {
72 return $from if $amount <= 0;
73 return $to if $amount >= 1;
74 my $mixed = 0;
75 foreach my $shift ( 16, 8, 0 ) {
76 my ( $start, $end ) = ( ( $from >> $shift ) & 0xFF, ( $to >> $shift ) & 0xFF );
77 $mixed |= int( $start + ( $end - $start ) * $amount + 0.5 ) << $shift;
78 }
79 return $mixed;
80 }
81  
82 # The relative luminance of WCAG 2: 0 for black, 1 for white.
83 sub luminance ($rgb) {
84 my @linear = map {
85 my $channel = ( ( $rgb >> $_ ) & 0xFF ) / 255;
86 $channel <= 0.04045 ? $channel / 12.92 : ( ( $channel + 0.055 ) / 1.055 )**2.4
87 } 16, 8, 0;
88 return 0.2126 * $linear[0] + 0.7152 * $linear[1] + 0.0722 * $linear[2];
89 }
90  
91 # Whether dark text reads better than light text on the color.
92 sub is_light_rgb ($rgb) {
93 my $luminance = luminance($rgb);
94 return ( 1.05 / ( $luminance + 0.05 ) ) < ( ( $luminance + 0.05 ) / 0.05 ) ? 1 : 0;
95 }
96  
97 # Text that stays readable on the color: near black or near white.
98 sub contrast_rgb ($rgb) {
99 return is_light_rgb($rgb) ? 0x111111 : 0xF5F5F5;
100 }
101  
102 # The colors of a chart's text and lines on a background in a mode:
103 # title, text (legend), label (ticks, axis titles), axis and grid.
104 sub ink_colors ( $background, $mode ) {
105 my $ink = $INK{ one_of( __PACKAGE__, mode => $mode, keys %INK ) };
106 return {
107 title => mix_rgb( $background, $ink, 0.95 ),
108 text => mix_rgb( $background, $ink, 0.80 ),
109 label => mix_rgb( $background, $ink, 0.55 ),
110 axis => mix_rgb( $background, $ink, 0.30 ),
111 grid => mix_rgb( $background, $ink, 0.12 ),
112 };
113 }
114  
115 sub rgb_hex ($rgb) {
116 return sprintf '#%06x', $rgb;
117 }
118  
119 1;
120  
121 __END__
« blib/lib/Term/Fabulous/Widget/Text.pm ↑ Summary blib/lib/Term/Fabulous/Check.pm »