merrymonk has asked for the wisdom of the Perl Monks concerning the following question:

Mastering Perl/Tk says that $widget->rgb(“colour”) gives values from 0 to 255 for the red, green and blue components of the colour.
As the attached Perl shows it seems to give a value approximately the square on these numbers.
I find I can get the range with the expression int(sqrt(red/green/blue)).
The output from the Perl is

colour <black> red <0> green <0> blue <0>
sqrt colour <black> red <0> green <0> blue <0>

colour <white> red <65535> green <65535> blue <65535>
sqrt colour <white> red <255> green <255> blue <255>

colour <red> red <65535> green <0> blue <0>
sqrt colour <red> red <255> green <0> blue <0>

colour <green> red <0> green <65535> blue <0>
sqrt colour <green> red <0> green <255> blue <0>

colour <blue> red <0> green <0> blue <65535>
sqrt colour <blue> red <0> green <0> blue <255>

Can anyone explain why is happening and/or is using the int/sqrt function is the correct thing to do to get the quoted range?
use Tk; use strict "vars"; my (@colours, $col_item, $red, $green, $blue, $mw); $mw = MainWindow->new; @colours = qw(black white red green blue); foreach $col_item (@colours) { ($red, $green, $blue) = $mw->rgb($col_item); print "\n colour <$col_item> red <$red> green <$green> blue <$blue +>\n"; $red = int(sqrt($red)); $green =int(sqrt($green)); $blue = int(sqrt($blue)); print "sqrt colour <$col_item> red <$red> green <$green> blue <$blue>\ +n"; } MainLoop;

Replies are listed 'Best First'.
Re: Perl/Tk - query about rdg values of a colour
by lamprecht (Friar) on Sep 04, 2009 at 07:41 UTC
    Hi,
    is using the int/sqrt function is the correct thing to do to get the quoted range?

    I'd rather divide by 256 . Otherwise you will get a non linear mapping.


    Cheers, Christoph
Re: Perl/Tk - query about rdg values of a colour
by Anonymous Monk on Sep 04, 2009 at 07:25 UTC
    Tk::Widget
    $widget->rgb(color)

    Returns a list containing three decimal values, which are the red, green, and blue intensities that correspond to color in the window given by $widget. Color may be specified in any of the forms acceptable for a color option.

Re: Perl/Tk - query about rdg values of a colour
by Anonymous Monk on Sep 04, 2009 at 07:16 UTC