http://qs1969.pair.com?node_id=11129205


in reply to Ordering Colours Problem

Colour value is a point in 3d space, how would you "order" these points along (which?) single axis? That said, the "ordering" can be considered as TSP, where simple greedy nearest neighbour algorithm and arbitrarily/heuristically chosen starting point (black) produce "good enough" result. (The fact that black is among source colours (moreover, first) is irrelevant -- root isn't included in solution)

Here are 5 colour ramps:

Well, how much scientific ramp is more "pleasing" is in the eye of beholder :). Maybe dumb NN is limiting factor here.

use strict; use warnings; use feature 'say'; use Imager; use Convert::Color; use Convert::Color::RGB8; my @colors = map tr/#//dr, split "\n", <<'END_OF_COLORS'; #000000 #716373 #704A2B #AF7E45 #963049 #AA2261 #B24551 #E6212E #FF0000 #001200 #FFDE72 #F55B73 END_OF_COLORS my $HEIGHT = 50; my $TILE_W = 50; my $WIDTH = $TILE_W * @colors; my $START = '000000'; my $c = Convert::Color::RGB8-> new( $START ) # -> convert_to( 'hsv' ); # 1 my @data = map [ $_, Convert::Color::RGB8-> new( $_ )-> convert_to( 'hsv' ), # 2 0 ], @colors; my @sorted; my $len = 0; while ( @data ) { $_-> [ 2 ] = $_-> [ 1 ]-> dst_hsv( $c ) # 3 for @data; @data = sort { $a-> [ 2 ] <=> $b-> [ 2 ] } @data; $len += $data[ 0 ][ 2 ]; ( my $str, $c ) = @{ shift @data }; push @sorted, $str; } say $len; my $im = Imager-> new( xsize => $WIDTH, ysize => $HEIGHT, model => 'rgb' ); my $x = 0; for ( @sorted ) { $im-> box( filled => 1, xmin => $x, color => $_ ); $x += $TILE_W } $im-> write( file => 'result.png' ); __END__ use PDL; use PDL::Transform::Color; use PDL::Graphics::ColorDistance; my $c = t_lab-> apply((!t_srgb)-> apply( # pdl [ Convert::Color::RGB8-> new( $START )-> rgb8 ])); # 1 t_lab-> apply((!t_srgb)-> apply( # pdl [ Convert::Color::RGB8-> new( $_ )-> rgb8 ])), # 2 $_-> [ 2 ] = delta_e_2000( $_-> [ 1 ], $c ) # 3

Edit: s/Through/Throw/ :)

Replies are listed 'Best First'.
Re^2: Ordering Colours Problem
by choroba (Archbishop) on Mar 06, 2021 at 22:54 UTC
    > Black appearing second after red is puzzling, though

    If you run my code several times, you'll see the black and red are sometimes swapped. That's because black is in fact the darkest possible red :-)

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      Oh, I see: sort is performed on keys of derived hash, not original list. Hence the unstable result.

      In the page you linked to one finds the HLS model visualized as a cylinder = 2d-circle x height. (A round cake )

      While height is (L)umidity, does the distance to the circles center represent (S)aturation.

      The axis of that cylinder, the circles center (S=0) is the gray-scale, with black at the bottom (L=0) and white at the top (L=1) and belongs to all "colors" of that wheel.

      I.e one point on the wheel represents all colors in the rectangle formed by axis and angle. (The side of a cakepiece)

      Crazy!

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery