Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Generating Visually Distinct Colors

by iblech (Friar)
on Mar 22, 2005 at 15:36 UTC ( [id://441502]=note: print w/replies, xml ) Need Help??


in reply to Generating Visually Distinct Colors

Perl 6! :) (code slightly adapted to run under Pugs)

#!/usr/bin/perl6 use v6; # ceil() not yet implemented in Pugs sub ceil ($n) { int($n) + ($n > int($n) ?? 1 :: 0) } my $n = 10; my $discrete = ceil($n ** (1/3)); my $r = any 0..$discrete-1; my $g = any 0..$discrete-1; my $b = any 0..$discrete-1; # Now $r,$g,$b are Junctions containing all the possible values of # red/green/blue. $r = int( (1-$r/$discrete) * 255 ); $g = int( (1-$g/$discrete) * 255 ); $b = int( (1-$b/$discrete) * 255 ); # $color is a Junction containing all colors as "(red, green, blue)". my $color = "($r, $g, $b)"; # Finally, print $color. for $color -> $x { say $x }

Update: Hand-rolled ceil was wrong, fixed (thanks japhy!).

Replies are listed 'Best First'.
Re^2: Generating Visually Distinct Colors
by japhy (Canon) on Mar 22, 2005 at 16:46 UTC
    Um, what's with that ceil() function? int($x+2)? I'd feel much safer with
    sub ceil (Num $x) { $x == int($x) ?? $x :: int($x+1) }
    I'm pretty sure that's the appropriate P6 code.
    _____________________________________________________
    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
      In Perl 5, ceil is in package POSIX and it is (together with floor) the recommended way to do a unilateral round towards an integer. For what I can remember, the use of int for this is discouraged even in Perl (5) documentation, due to the fact that its behaviour is not consistent between positive and negative numbers (it only cuts decimals away).

      I don't know if this has been changed in Perl 6, but I think that I'll stick to the library ceil anyway.

      Flavio

      -- Don't fool yourself.
        Yes, I use ceil(), I was commenting on the very bizarre P6 ceil-equivalent.
        _____________________________________________________
        Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
        How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re^2: Generating Visually Distinct Colors
by Roy Johnson (Monsignor) on Mar 22, 2005 at 21:32 UTC
    Does that output $n colors, or $n rounded to the next higher cube?

    Caution: Contents may have been coded under pressure.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://441502]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-19 11:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found