Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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:

  • Original (unordered)
  • Sorted just by hue, according to choroba. Not for any absurd competition, but for illustration only, since there's png generating code below, anyway. Black appearing second after red is puzzling, though
  • NN result, where metric is euclidean distance in HSV space. See code. See Convert::Color
  • NN result, where metric is euclidean distance in HSL space (s/hsv/hsl/g in code below)
  • Throw some hard science at the problem. HSV/HSL are not perceptually uniform. Assuming RGB is sRGB, map colours to Lab colour space (a). At the time of its introduction, this space was designed to be perceptually uniform, but practice revealed it isn't. Therefore, let metric be not euclidean distance in Lab (CIEdE), but adjusted CIEdE2000 (b). Conversion (a) and metric (b) are not too difficult to implement in Perl by hand, but we have PDL::Transform::Color and PDL::Graphics::ColorDistance, so why not use them. See addendum after __END__, add use statements and replace 1,2,3 fragments with their counterparts.

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/ :)


In reply to Re: Ordering Colours Problem by vr
in thread Ordering Colours Problem by merrymonk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-03-28 20:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found