190331.11 · 0f61656bc0c72adc414ddf49b8e705e58c549950
Added Bill's changes to organize RGB chart by HSL.
reading declarations
@@ -0,0 +1,149 @@+#! perl+# Show Term::ANSIColor 216-cube of colors 000..555 arrayed as HSL grid+#+# Copyright 2019 William Ricker modifying code by Andy Lester+# License Same As Perl++use strict;+use warnings;+use 5.010001;++use Convert::Color;+use Convert::Color::HSL;+use Convert::Color::RGB;++use Term::ANSIColor;++use Readonly;+my Readonly $degrees = 12; ## This is the number that avoids collisions+my Readonly $columns = 360 / $degrees;+++my $where_href = _save_rgb_grid();+_show_hue_sat_grid($where_href);+++# Helper to scale Term::ANSIColor 0..5 R,G,B to 0.0 .. 1.0 standard+sub _five_to_1{+ return shift()/5;+}+++sub _where {+ # pass in HSL object, returns xyz grid to display it on+ # if $degrees selected properly, won't cause collisions+ my $hsl = shift;+ my ($h, $s, $l) = $hsl->hsl;+ my $c = $hsl->chroma;+ my $y = int 10*$l ;+ my $z = int 10*$s ;+ my $x = sprintf "%0d", ($degrees * int( ($h+($degrees/2))/$degrees ));+ return [ $x, $y, $z] ;+}+++## HSL "Grid"++# show Hue Sat Lum grid+# Modeled on Ack3 _show_rgb_grid, but with HSL iteration over sparce save matrix+# If a position is blank, repeat previous color+#+# Because 20 columns wide,+# omits 'rgb' prefix+# stacks text over background+#+# arguments+# Where = the hashref returned by sibling+#+#+sub _show_hue_sat_grid {+ my $where = shift;++ my (%Where) = ($where->%*);++ my @Hues = map { $degrees * $_ } 0 .. ($columns-1);+ for my $z (sort {$b <=> $a} keys %Where){+ # say "s $z" ; # dd $Where{$z};+ for my $y (sort {$b <=> $a} keys $Where{$z}->%*){+ # say "l $y"; # dd $Where{$z}->{$y};+ my $code='rgb000'; # default if hue=0 ever missing+ for my $x (@Hues) {+ my $skippable;+ if ( defined $Where{$z}->{$y}->{$x}) {+ $code = $Where{$z}->{$y}->{$x}->[0]->{code} // $code; ## repeat if position not used+ }+ print substr( $code, 3, 3 ), ' ';+ }+ say '';+ }+ say '';+ }+ }+++ # Save the RGB Grid values into HSL grid+ # This saves a sparse matrix in nested hash form+ #+ # Modeled on Ack3 _show_rgb_grid, but with HSL and save instead of print+ # saves into %Where for sibling+sub _save_rgb_grid {+ # Optional statistics+ # my (%Hues,%Lums,%Sats);+ my %Where;+ for my $r ( 0 .. 5 ) {+ for my $g ( 0 .. 5 ) {+ for my $b ( 0 ..5 ) {++ my $rgb = "$r$g$b";+ my $code = "rgb$r$g$b";+ my $hsl = Convert::Color::RGB->new(map {(_five_to_1($_))} ($r, $g, $b))->as_hsl;+ my @HSL = ($hsl->hsl);+ # Optional collect stats+ # my { ($h,$s,$l)=@HSL; $Hues{int $h+0.5}++; $Lums{$l}++; $Sats{$s}++; }+ my $hsl_code = sprintf q(h:%3d,s:%4.2f,l:%4.2f), @HSL;+ my ($x,$y, $z) = _where($hsl)->@*;+# warn "Mapping white to [$x,$y, $z]" if 5==$r and 5==$g and 5==$b;+# warn "[$x,$y] conflict #{[$Where{$y}->{$x}]} = $code (h=$HSL[0])"+# if defined $Where{$y}->{$x} ;+ push $Where{$z}->{$y}->{$x}->@* , { code => $code, hsl=> $hsl } ;++ }+ }+ }++ # say "Hues"; for my $k (sort {$a <=> $b} keys %Hues){ say "$Hues{$k}\t$k"; }+ # say "Lums"; for my $k (sort {$a <=> $b} keys %Lums){ say "$Lums{$k}\t$k"; }+ # say "Sats"; for my $k (sort {$a <=> $b}keys %Sats){ say "$Sats{$k}\t$k"; }+ return \%Where;+}++++=for samples++# Inspecting Convert::Color POD sample values deeply++use Data::Dump qw/dd/;++my $red = Convert::Color::HSL->new( 0, 1, 0.5 );++# Can also parse strings+say q(pink);+my $pink = Convert::Color::HSL->new( '0,1,0.8' );+dd $pink;+dd $pink->as_rgb;+dd $pink->as_rgb->as_hsl;++my $cyan = Convert::Color->new( 'hsl:300,1,0.5' );++say q(cyan);+dd $cyan;+dd $cyan->as_rgb;++say "132";+my $onethreetwo= Convert::Color::RGB->new(map {(_five_to_1($_))} (1, 3, 2));+dd $onethreetwo;+dd $onethreetwo->as_hsl->hsl;+dd $onethreetwo->as_hsl->chroma;++=cut
@@ -469,12 +469,19 @@ sub show_help_rgb { ack allows customization of the colors it uses when presenting matches onscreen. See the "ACK COLORS" section of the ack manual (ack --man). -Here is a grid of the 216 possible RGB colors. For the standard color-grid, run ack --help-colors.+Colors may be specified as "rggNNN" where "NNN" is a triplet of digits+from 0 to 5 specifying the intensity of red, green and blue, respectively.++Here is a grid of the 216 possible values for NNN.+ END_OF_HELP _show_rgb_grid(); + App::Ack::say( 'Here are the 216 possible colors with the "reverse" modifier applied.', "\n" );++ _show_rgb_grid( 'reverse' );+ return; } @@ -526,21 +533,44 @@ sub _color_cell { sub _show_rgb_grid {- for my $r ( 0 .. 5 ) {- for my $g ( 0 .. 5 ) {- for my $b ( 0 ..5 ) {- my $rgb = "$r$g$b";- my $code = "rgb$r$g$b";-- App::Ack::print(- Term::ANSIColor::colored( $code, $code ),- Term::ANSIColor::colored( $code, "reverse $code" ),- ' '- );- }- App::Ack::say( '' );- }- }+ my $modifier = shift // '';++ my $grid = <<'HERE';+544 544 544 544 544 554 554 554 554 554 454 454 454 454 454 455 455 455 455 455 445 445 445 445 445 545 545 545 545 545+533 533 533 543 543 553 553 553 453 453 353 353 353 354 354 355 355 355 345 345 335 335 335 435 435 535 535 535 534 534+511 521 531 531 541 551 451 451 351 251 151 152 152 153 154 155 145 145 135 125 115 215 215 315 415 515 514 514 513 512+500 510 520 530 540 550 450 350 250 150 050 051 052 053 054 055 045 035 025 015 005 105 205 305 405 505 504 503 502 501+400 410 410 420 430 440 340 340 240 140 040 041 041 042 043 044 034 034 024 014 004 104 104 204 304 404 403 403 402 401+300 300 310 320 320 330 330 230 130 130 030 030 031 032 032 033 033 023 013 013 003 003 103 203 203 303 303 302 301 301+200 200 200 210 210 220 220 220 120 120 020 020 020 021 021 022 022 022 012 012 002 002 002 102 102 202 202 202 201 201+100 100 100 100 100 110 110 110 110 110 010 010 010 010 010 011 011 011 011 011 001 001 001 001 001 101 101 101 101 101++522 522 532 542 542 552 552 452 352 352 252 252 253 254 254 255 255 245 235 235 225 225 325 425 425 525 525 524 523 523++411 411 421 431 431 441 441 341 241 241 141 141 142 143 143 144 144 134 124 124 114 114 214 314 314 414 414 413 412 412++422 422 432 432 432 442 442 442 342 342 242 242 242 243 243 244 244 244 234 234 224 224 224 324 324 424 424 424 423 423++311 311 311 321 321 331 331 331 231 231 131 131 131 132 132 133 133 133 123 123 113 113 113 213 213 313 313 313 312 312++433 433 433 433 433 443 443 443 443 443 343 343 343 343 343 344 344 344 344 344 334 334 334 334 334 434 434 434 434 434+211 211 211 211 211 221 221 221 221 221 121 121 121 121 121 122 122 122 122 122 112 112 112 112 112 212 212 212 212 212++322 322 322 322 322 332 332 332 332 332 232 232 232 232 232 233 233 233 233 233 223 223 223 223 223 323 323 323 323 323++555 555 555 555 555 555 555 555 555 555 555 555 555 555 555 555 555 555 555 555 555 555 555 555 555 555 555 555 555 555+444 444 444 444 444 444 444 444 444 444 444 444 444 444 444 444 444 444 444 444 444 444 444 444 444 444 444 444 444 444+333 333 333 333 333 333 333 333 333 333 333 333 333 333 333 333 333 333 333 333 333 333 333 333 333 333 333 333 333 333+222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222+111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111+000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000+HERE++ $grid =~ s/(\d\d\d)/Term::ANSIColor::colored( "$1", "$modifier rgb$1" )/eg;++ App::Ack::say( $grid );++ return; }