HTML Color Codes

Sarah Reichelt sarah.reichelt at gmail.com
Thu Aug 7 00:39:04 EDT 2008


> I was wondering how to translate the RGB names to the HTML equivalent when user selects color with the Color Dialog. For such use as with a html page generator.
>
> If it doesn't exist already (though surely it's been done before, and I just can't find it): am I on the right track thinking that an array would be used to hold both code sets, then a function returns the html equivalent rather than the RGB? Or something!

HTML colors are just the same as RGB colors but with the decimal
numbers converted to hexadecimal and then run together. So you can
calculate one from the other and don't need an array or lookup table.
Here is a function for doing the conversion.

function RGBtoHTML pRGB
    put item 1 of pRGB into r
    put item 2 of pRGB into g
    put item 3 of pRGB into b

    put baseconvert(r,10,16) into r
    put baseconvert(g,10,16) into g
    put baseconvert(b,10,16) into b

    return "#" & r & g & b
end RGBtoHTML

So you can do something like this:

answer color
put it into tRGBcolor
put RGBtoHTML(tRGBcolor) into tHTMLcolor

Cheers,
Sarah



More information about the use-livecode mailing list