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

I need help on writing a script that will convert rgb to hex color values. Ultimately, I'd like to be able to present a little color swatch that the user can click on to receive the chosen color's hex value. Any easy solutions?

Replies are listed 'Best First'.
Re: hex color generator
by amelinda (Friar) on Oct 09, 2000 at 22:46 UTC
    I'm curious; why are you replicating something that has already been done? There are many web sites out there which map colors to hex values for the web. Try a Google search for "web-safe color wheel hex". The first one that search returns is a neat little java applet which also shows you what it would look like on an actual page.

    I was about to say "perhaps you wish to convert arbitrary decimal rbg values to hex rbg values and that's a different matter," but if this is for a CGI script, the output for colors not in the "web-safe" palette will vary from monitor to monitor. Thus, making arbitrary colors available is probably making much more work than is necessary for what you seem to have in mind.

Re: hex color generator
by merlyn (Sage) on Oct 09, 2000 at 20:30 UTC
    First, you haven't said whether this is a web question or not. Could be web, could be TK, could be something else entirely. But let's presume web since that's what most people mean when they don't say since they don't think anything exists besides the web. {grin}

    Depending on how cut-n-paste-and-clickable you want, you can use something like GD or Image::Magick to generate a real image, or you can use a BGCOLOR attribute of a table cell to generate a color.

    More details upon request; please include your existing knowledge so we don't repeat what you already have discovered.

    -- Randal L. Schwartz, Perl hacker

Re: hex color generator
by wardk (Deacon) on Oct 09, 2000 at 21:50 UTC

    If you create a CGI script to produce the colors, you'd probably be looping through the possible combinations. Since you need the combinations to create the color, you could use the color in the link back to your CGI, which would then "know" that the hex value submitted "matched" the color picked.

    happy coding

RE: hex color generator
by acid06 (Friar) on Oct 10, 2000 at 09:03 UTC
    Converting RGB to HEX is easy, you can use something like this:
    my ($red,$green,$blue) = (100,100,100); #or whatever $hex = sprintf("%x%x%x",$red,$green,$blue);
    Then $hex would be assigned to the hex value of the desired RGB color ($red, $green, $blue).
    The UI is up to you.

    "What once was, was, and won't be again."
      You want that sprintf string to be "%02x%02x%02x" so that zeropadding is turned on and set to 2 chars width. otherwise (255,128,9) gets you 'ff809' which may not be too useful where a 6 char value is required. =)

      --
      $you = new YOU;
      honk() if $you->love(perl)

Re: hex color generator
by Anonymous Monk on Oct 09, 2000 at 20:42 UTC
    Requesting more knowledge. Existing knowledge on this subject is at its minimum, so I'm quite sure what you tell me will be new for me. By the way, this is for the web.