1

How can I convert RGB values into HTML code?

For example, using values like this:

red:0
blue:0
green:0
html color code = #000000

Is there any formula for converting it?

7
  • 3
    First google result: haacked.com/archive/2009/12/29/convert-rgb-to-hex.aspx Commented Aug 3, 2011 at 0:30
  • Do your red, green, and blue value range from 0-1 or 0-255? Commented Aug 3, 2011 at 0:30
  • @citizen conn - was about to post the very same link :) Commented Aug 3, 2011 at 0:31
  • yes the value range from 0 - 255 Commented Aug 3, 2011 at 0:32
  • 1
    Do you specifically need the hex code? You can actually just use the rgb values in CSS e.g. color: rgb(0,0,0); Commented Aug 3, 2011 at 0:39

4 Answers 4

4

If you don't specifically need to use the hex value (html colour code) you can actually just use the rgb values you already have. Live example: http://jsfiddle.net/DdFg8/1/

color: rgb(0,0,0); /* black */

color: rgb(255,255,255); /* white */
Sign up to request clarification or add additional context in comments.

Comments

2

You just need to convert each component value to its corresponding hex representation, and concatenate them into a string, like:

function colorCode(red, green, blue) {
   red = normalize(red);
   green = normalize(green);
   blue = normalize(blue);

   return '#' + pad(red.toString(16)) + pad(green.toString(16)) + pad(blue.toString(16)); 
}

function pad(string) {
    return string.length > 1 ? string.toUpperCase() : "0" + string.toUpperCase();
}

function normalize(color) {
    return (color < 1.0 && color > 0.0) ? Math.floor(color * 255) : color;
}

Example: http://jsfiddle.net/5TdXJ/2

Comments

0

the HTML representation is just a hex represntation of the rgb values

python -c 'print "%x" % (255)'  # prints ff

Comments

0

If you search "Convert RGB to Hex" on Google you will see a tons of online converter. What HTML (that # sign string) is a Hex presentation of color, where the first two hex digit represents Red, the middle two digit represents Green and the last two digit represent Blue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.