| 1 | |||||||||||||||||||||||||||||
| 2 | # color value operation generating color sets | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | package Graphics::Toolkit::Color::SetCalculator; | ||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use v5.12; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use Graphics::Toolkit::Color::Calculator; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | ######################################################################## | ||||||||||||||||||||||||||||
| 10 | 14 | ▶ | sub complement { # -- :start_values, @target_delta, +steps, +tilt, +skew, :space --> @:values | ||||||||||||||||||||||||||
|
Subroutine
complement: called CC=14 SCAR=26.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 11 | 14 | my ($start_color, $target_delta, $steps, $tilt, $skew, $color_space) = @_; | |||||||||||||||||||||||||||
| 12 | 14 | ▶ | return unless ref $color_space eq 'Graphics::Toolkit::Color::Space'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 13 | 14 | ▶ | return 'need a cylindrical color space from the HSL family as color space' unless $color_space->family eq 'HSL'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 14 | 14 | my $axis_position = { | |||||||||||||||||||||||||||
| 15 | h => $color_space->pos_from_axis_role('hue'), | ||||||||||||||||||||||||||||
| 16 | s => $color_space->pos_from_axis_role('saturation'), | ||||||||||||||||||||||||||||
| 17 | l => $color_space->pos_from_axis_role('lightness'), | ||||||||||||||||||||||||||||
| 18 | }; | ||||||||||||||||||||||||||||
| 19 | 14 | my $hue_half_max = $color_space->shape->axis_value_max( $axis_position->{'h'} ) / 2; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 14 | my $start_tuple = $start_color->shaped( $color_space->name ); | |||||||||||||||||||||||||||
| 22 | 14 | $start_tuple = $color_space->rotate( $start_tuple ); | |||||||||||||||||||||||||||
| 23 | 14 | my $target_values = [@$start_tuple]; # target = THE complement + usr changes | |||||||||||||||||||||||||||
| 24 | 14 | $target_values->[$axis_position->{'h'}] += $hue_half_max; | |||||||||||||||||||||||||||
| 25 | 14 | ▶ | $target_delta->[$_] //= 0 for 0 .. 2; | ||||||||||||||||||||||||||
|
Condition100%2/2
$target_delta->[$_] //= 0
MC/DC100%1/1
$target_delta->[$_] //= 0
$target_delta->[$_]
Truth table100%2/2
A
A= $target_delta->[$_]
| |||||||||||||||||||||||||||||
| 26 | 14 | $target_values->[$_] += $target_delta->[$_] for 0 .. 2; | |||||||||||||||||||||||||||
| 27 | 14 | $target_values = $color_space->clamp( $target_values ); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 14 | $target_delta->[$axis_position->{'s'}] = $target_values->[$axis_position->{'s'}] - $start_tuple->[$axis_position->{'s'}]; | |||||||||||||||||||||||||||
| 30 | 14 | $target_delta->[$axis_position->{'l'}] = $target_values->[$axis_position->{'l'}] - $start_tuple->[$axis_position->{'l'}]; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 14 | my $result_count = int abs $steps; | |||||||||||||||||||||||||||
| 33 | 14 | my $scaling_exponent = abs($tilt) + 1; | |||||||||||||||||||||||||||
| 34 | 14 | my @hue_pos_normal = map {($_ * 2 / $result_count) ** $scaling_exponent} 1 .. ($result_count - 1) / 2; | |||||||||||||||||||||||||||
| 35 | 14 | ▶ | @hue_pos_normal = map {1 - $_} reverse @hue_pos_normal if $tilt > 0; # reverse tilt effect if tilt negative | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 14 | my $hue_target_delta = $hue_half_max + $target_delta->[$axis_position->{'h'}]; # real value size of half complement circle | |||||||||||||||||||||||||||
| 38 | 14 | my @result = (); | |||||||||||||||||||||||||||
| 39 | 14 | for my $hue_position (@hue_pos_normal){ | |||||||||||||||||||||||||||
| 40 | 10 | my $tuple = []; | |||||||||||||||||||||||||||
| 41 | 10 | $tuple->[$axis_position->{'h'}] = $start_tuple->[$axis_position->{'h'}] + ($hue_target_delta * $hue_position); | |||||||||||||||||||||||||||
| 42 | 10 | $tuple->[$axis_position->{'s'}] = $start_tuple->[$axis_position->{'s'}] + ($target_delta->[$axis_position->{'s'}] * $hue_position); | |||||||||||||||||||||||||||
| 43 | 10 | $tuple->[$axis_position->{'l'}] = $start_tuple->[$axis_position->{'l'}] + ($target_delta->[$axis_position->{'l'}] * $hue_position); | |||||||||||||||||||||||||||
| 44 | 10 | ▶ | $tuple->[$axis_position->{'l'}] -= ($hue_position <= 0.5) ? ($skew * $hue_position * 2) : ($skew * (2 - ( $hue_position * 2))); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 10 | push @result, Graphics::Toolkit::Color::Values->new_from_tuple( $tuple, $color_space->name ); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | 14 | ▶ | push @result, Graphics::Toolkit::Color::Values->new_from_tuple( $target_values, $color_space->name) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$result_count == 1 or not $result_count % 2
MC/DC100%2/2
$result_count == 1 or not $result_count % 2
$result_count == 1
not $result_count % 2
Truth table100%3/3
A or not(B)
A= $result_count == 1B= not $result_count % 2
| |||||||||||||||||||||||||||||
| 48 | if $result_count == 1 or not $result_count % 2; | ||||||||||||||||||||||||||||
| 49 | 14 | $hue_target_delta = $hue_half_max - $target_delta->[$axis_position->{'h'}]; | |||||||||||||||||||||||||||
| 50 | 14 | @hue_pos_normal = map {1 - $_} reverse @hue_pos_normal; | |||||||||||||||||||||||||||
| 51 | 14 | for my $hue_position (@hue_pos_normal){ | |||||||||||||||||||||||||||
| 52 | 10 | my $tuple = []; | |||||||||||||||||||||||||||
| 53 | 10 | $tuple->[$axis_position->{'h'}] = $target_values->[$axis_position->{'h'}] + ($hue_target_delta * $hue_position); | |||||||||||||||||||||||||||
| 54 | 10 | $tuple->[$axis_position->{'s'}] = $target_values->[$axis_position->{'s'}] - ($target_delta->[$axis_position->{'s'}] * $hue_position); | |||||||||||||||||||||||||||
| 55 | 10 | $tuple->[$axis_position->{'l'}] = $target_values->[$axis_position->{'l'}] - ($target_delta->[$axis_position->{'l'}] * $hue_position); | |||||||||||||||||||||||||||
| 56 | 10 | ▶ | $tuple->[$axis_position->{'l'}] += ($hue_position <= 0.5) ? ($skew * $hue_position * 2) : ($skew * (2 - ( $hue_position * 2))); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 10 | push @result, Graphics::Toolkit::Color::Values->new_from_tuple( $tuple, $color_space->name ); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | 14 | ▶ | push @result, $start_color if $result_count > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 14 | return @result; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | ######################################################################## | ||||||||||||||||||||||||||||
| 64 | 3 | ▶ | sub analogous { # :start_values, :next_values -- +steps, +tilt, :space --> @:values | ||||||||||||||||||||||||||
|
Subroutine
analogous: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 65 | 3 | my ($start_color, $next_color, $steps, $tilt, $color_space) = @_; | |||||||||||||||||||||||||||
| 66 | 3 | $steps = int $steps; | |||||||||||||||||||||||||||
| 67 | 3 | ▶ | return $start_color if $steps == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 3 | ▶ | return $start_color, $next_color if $steps == 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 3 | my @result = ($start_color, $next_color); | |||||||||||||||||||||||||||
| 70 | 3 | my $start_tuple = $start_color->normalized( $color_space->name ); | |||||||||||||||||||||||||||
| 71 | 3 | my $next_tuple = $next_color->normalized( $color_space->name ); | |||||||||||||||||||||||||||
| 72 | 3 | my $delta_tuple = $color_space->delta( $start_tuple, $next_tuple ); | |||||||||||||||||||||||||||
| 73 | 3 | for my $color_nr (3 .. $steps){ | |||||||||||||||||||||||||||
| 74 | 4 | for my $axis_nr ($color_space->basis->axis_iterator){ | |||||||||||||||||||||||||||
| 75 | 12 | $delta_tuple->[$axis_nr] *= 1 + $tilt; | |||||||||||||||||||||||||||
| 76 | 12 | $next_tuple->[$axis_nr] += $delta_tuple->[$axis_nr]; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | 4 | my $next_color = $start_color->new_from_tuple( $next_tuple, $color_space->name, 'normal', 'raw' ); | |||||||||||||||||||||||||||
| 79 | 4 | ▶ | last unless $next_color->is_in_gamut( $color_space->name ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 3 | push @result, $next_color; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 3 | return @result; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | ######################################################################## | ||||||||||||||||||||||||||||
| 86 | 15 | ▶ | sub gradient { # @:color_values -- +steps, +tilt, :space --> @:values | ||||||||||||||||||||||||||
|
Subroutine
gradient: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 87 | 15 | my ($colors, $steps, $tilt, $color_space) = @_; | |||||||||||||||||||||||||||
| 88 | 15 | my $scaling_exponent = abs($tilt) + 1; # tilt = exponential scaling | |||||||||||||||||||||||||||
| 89 | 15 | my $segment_count = @$colors - 1; | |||||||||||||||||||||||||||
| 90 | 15 | my @percent_in_gradient = map {(($_-1) / ($steps-1)) ** $scaling_exponent} 2 .. $steps - 1; | |||||||||||||||||||||||||||
| 91 | 15 | ▶ | @percent_in_gradient = map {1 - $_} reverse @percent_in_gradient if $tilt < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 15 | my @result = ($colors->[0]); | |||||||||||||||||||||||||||
| 93 | 15 | for my $step_nr (2 .. $steps - 1){ | |||||||||||||||||||||||||||
| 94 | 55 | my $percent_in_gradient = $percent_in_gradient[$step_nr-2]; | |||||||||||||||||||||||||||
| 95 | 55 | my $current_segment_nr = int ($percent_in_gradient * $segment_count); | |||||||||||||||||||||||||||
| 96 | 55 | my $percent_in_segment = $segment_count * ($percent_in_gradient - ($current_segment_nr / $segment_count)); | |||||||||||||||||||||||||||
| 97 | 55 | push @result, Graphics::Toolkit::Color::Calculator::mix( | |||||||||||||||||||||||||||
| 98 | $colors->[$current_segment_nr], [ $colors->[$current_segment_nr+1] ], $percent_in_segment, $color_space ); | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 15 | ▶ | push @result, pop @$colors if $steps > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 15 | return @result; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | ######################################################################## | ||||||||||||||||||||||||||||
| 105 | 6 | my $adj_len_at_45_deg = sqrt(2) / 2; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 16 | ▶ | sub cluster { # :center_values, @+|+radius +min_distance -- :space --> @:values | ||||||||||||||||||||||||||
|
Subroutine
cluster: called CC=16 SCAR=27.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 108 | 16 | my ($center_color, $cluster_radius, $color_distance, $color_space) = @_; | |||||||||||||||||||||||||||
| 109 | 16 | my $center_tuple = $center_color->shaped( $color_space->name ); | |||||||||||||||||||||||||||
| 110 | 16 | my $center_x = $center_tuple->[0]; | |||||||||||||||||||||||||||
| 111 | 16 | my $center_y = $center_tuple->[1]; | |||||||||||||||||||||||||||
| 112 | 16 | my $center_z = $center_tuple->[2]; | |||||||||||||||||||||||||||
| 113 | 16 | my @result_values; | |||||||||||||||||||||||||||
| 114 | 16 | ▶ | if (ref $cluster_radius) { # cuboid shaped cluster | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 8 | my $colors_in_direction = int $cluster_radius->[0] / $color_distance; | |||||||||||||||||||||||||||
| 116 | 8 | my $corner_value = $center_tuple->[0] - ($colors_in_direction * $color_distance); | |||||||||||||||||||||||||||
| 117 | 8 | @result_values = map {[$corner_value + ($_ * $color_distance)]} 0 .. 2 * $colors_in_direction; | |||||||||||||||||||||||||||
| 118 | 8 | for my $axis_index (1 .. $color_space->axis_count - 1){ | |||||||||||||||||||||||||||
| 119 | 17 | my $colors_in_direction = int $cluster_radius->[$axis_index] / $color_distance; | |||||||||||||||||||||||||||
| 120 | 17 | my $corner_value = $center_tuple->[$axis_index] - ($colors_in_direction * $color_distance); | |||||||||||||||||||||||||||
| 121 | @result_values = map { | ||||||||||||||||||||||||||||
| 122 | 17 | my @good_values = @$_[0 .. $axis_index-1]; | |||||||||||||||||||||||||||
| 123 | 77 | map {[@good_values, ($corner_value + ($_ * $color_distance))]} 0 .. 2 * $colors_in_direction; | |||||||||||||||||||||||||||
| 124 | } @result_values; | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | } else { # ball shaped cluster (FCC) | ||||||||||||||||||||||||||||
| 127 | 8 | my $layer_distance = sqrt( 2 * $color_distance * $color_distance ) / 2; | |||||||||||||||||||||||||||
| 128 | 8 | for my $layer_nr (0 .. $cluster_radius / $layer_distance){ | |||||||||||||||||||||||||||
| 129 | 16 | my $layer_height = $layer_nr * $layer_distance; | |||||||||||||||||||||||||||
| 130 | 16 | my $layer_z_up = $center_z + $layer_height; | |||||||||||||||||||||||||||
| 131 | 16 | my $layer_z_dn = $center_z - $layer_height; | |||||||||||||||||||||||||||
| 132 | 16 | my $layer_radius = sqrt( ($cluster_radius**2) - ($layer_height**2) ); | |||||||||||||||||||||||||||
| 133 | 16 | my $radius_in_colors = $layer_radius / $color_distance; | |||||||||||||||||||||||||||
| 134 | 16 | ▶ | if ($layer_nr % 2){ # odd layer of cuboctahedral packing | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 7 | my $contour_cursor = int ($radius_in_colors - 0.5); | |||||||||||||||||||||||||||
| 136 | 7 | my $grid_row_count = ($radius_in_colors * $adj_len_at_45_deg) - .5; | |||||||||||||||||||||||||||
| 137 | 7 | ▶ | next if $grid_row_count < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 7 | my @grid = (); | |||||||||||||||||||||||||||
| 139 | 7 | for my $x_index (0 .. $grid_row_count){ | |||||||||||||||||||||||||||
| 140 | 7 | ▶ | $contour_cursor-- if sqrt( (($contour_cursor+.5)**2) + (($x_index+.5)**2) ) > $radius_in_colors; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 7 | $grid[$x_index] = $contour_cursor; | |||||||||||||||||||||||||||
| 142 | 7 | $grid[$contour_cursor] = $x_index; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | 7 | for my $x_index (0 .. $#grid){ | |||||||||||||||||||||||||||
| 145 | 8 | my $delta_x = (0.5 + $x_index) * $color_distance; | |||||||||||||||||||||||||||
| 146 | 8 | my ($x1, $x2) = ($center_x + $delta_x, $center_x - $delta_x); | |||||||||||||||||||||||||||
| 147 | 8 | for my $y_index (0 .. $grid[$x_index]){ | |||||||||||||||||||||||||||
| 148 | 9 | my $delta_y = (0.5 + $y_index) * $color_distance; | |||||||||||||||||||||||||||
| 149 | 9 | my ($y1, $y2) = ($center_y + $delta_y, $center_y - $delta_y); | |||||||||||||||||||||||||||
| 150 | 9 | push @result_values, | |||||||||||||||||||||||||||
| 151 | [$x1, $y1, $layer_z_up], [$x2, $y1, $layer_z_up], | ||||||||||||||||||||||||||||
| 152 | [$x1, $y2, $layer_z_up], [$x2, $y2, $layer_z_up], | ||||||||||||||||||||||||||||
| 153 | [$x1, $y1, $layer_z_dn], [$x2, $y1, $layer_z_dn], | ||||||||||||||||||||||||||||
| 154 | [$x1, $y2, $layer_z_dn], [$x2, $y2, $layer_z_dn]; | ||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | } else { # even layer of cuboctahedral packing | ||||||||||||||||||||||||||||
| 158 | 9 | my $grid_row_count = int $radius_in_colors; | |||||||||||||||||||||||||||
| 159 | 9 | my @grid = ($grid_row_count); | |||||||||||||||||||||||||||
| 160 | 9 | $grid[$grid_row_count] = 0; | |||||||||||||||||||||||||||
| 161 | 9 | my $contour_cursor = $grid_row_count; | |||||||||||||||||||||||||||
| 162 | 9 | for my $x_index (1 .. $layer_radius * $adj_len_at_45_deg / $color_distance){ | |||||||||||||||||||||||||||
| 163 | 2 | ▶ | $contour_cursor-- if sqrt(($contour_cursor**2) + ($x_index**2)) > $radius_in_colors; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 2 | $grid[$x_index] = $contour_cursor; | |||||||||||||||||||||||||||
| 165 | 2 | $grid[$contour_cursor] = $x_index; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | 9 | my @layer_values = map {[$center_x + ($_ * $color_distance), $center_y, $layer_z_up]} | |||||||||||||||||||||||||||
| 168 | -$grid_row_count .. $grid_row_count; | ||||||||||||||||||||||||||||
| 169 | 9 | for my $y_index (1 .. $grid_row_count){ | |||||||||||||||||||||||||||
| 170 | 9 | my $delta_y = $y_index * $color_distance; | |||||||||||||||||||||||||||
| 171 | 9 | my ($y1, $y2) = ($center_y + $delta_y, $center_y - $delta_y); | |||||||||||||||||||||||||||
| 172 | 9 | for my $x_index (-$grid[$y_index] .. $grid[$y_index]){ | |||||||||||||||||||||||||||
| 173 | 11 | my $x = $center_x + ($x_index * $color_distance); | |||||||||||||||||||||||||||
| 174 | 11 | push @layer_values, [$x, $y1, $layer_z_up], [$x, $y2, $layer_z_up]; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | 9 | ▶ | if ($layer_nr > 0){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 1 | push @result_values, [$_->[0], $_->[1], $layer_z_dn] for @layer_values; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | 9 | push @result_values, @layer_values; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | # check for linear space borders and constraints | ||||||||||||||||||||||||||||
| 185 | 281 | return map { Graphics::Toolkit::Color::Values->new_from_tuple( $_, $color_space->name )} | |||||||||||||||||||||||||||
| 186 | 16 | grep { $color_space->is_in_linear_bounds($_) } @result_values; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 6 | 1; | |||||||||||||||||||||||||||