Hexadecimal for RGB

14

How do you convert a Hexadecimal color ( #FFFFFF ) to RGB ( R ed, G reen, B p>

Example

    
asked by anonymous 13.11.2015 / 18:07

7 answers

17

It's a question more about math / computational math than programming. Here is my contribution:

The RGB code, as the name says, is made up of 3 elements: Red, Green and Blue. In this case, the format #ffffff is a hexadecimal representation of the color code, where each two letters represent a color (in order). In order to 'translate' this number, we must make a base change - from hexadecimal (16), to decimal (10).

The first 10 numbers of the hexadecimal base are equal to the decimal base (0 to 9); the remaining 6 are (and their decimal representation): A (10); B (11); C (12); D (13); E (14) and F (15).

The basic conversion should be done digit by digit of each color, from right to left. For example:

#ffffff:
ff = 15 * 16 ^ 0 + 15 * 16 ^ 1 = 15 * 1 + 15 * 16 = 255
Portanto, em decimal, é (255, 255, 255)

#f5ffff
f5 = 5 * 16 ^ 0 + 15 * 16 ^ 1 = 5 * 1 + 15 * 16 = 245
ff = 15 * 16 ^ 0 + 15 * 16 ^ 1 = 15 * 1 + 15 * 16 = 255
Portanto, em decimal é (245, 255, 255)
    
13.11.2015 / 19:06
11

In C #

var cor = System.Drawing.ColorTranslator.FromHtml("#FFFFFF");
var corEmRgb = System.Drawing.Color.FromArgb(cor.R, cor.G, cor.B);

And to do the other way around

var cor = Color.FromArgb(255, 255, 255);
var hex = cor.R.ToString("X2") + cor.G.ToString("X2") + cor.B.ToString("X2");
    
13.11.2015 / 18:16
10

In PHP

I removed the answer from here :

function hex2rgb($hex){
    $hex = str_replace("#", "", $hex);

    if(strlen($hex) == 3) {
        $r = hexdec(substr($hex,0,1).substr($hex,0,1));
        $g = hexdec(substr($hex,1,1).substr($hex,1,1));
        $b = hexdec(substr($hex,2,1).substr($hex,2,1));
    } else {
        $r = hexdec(substr($hex,0,2));
        $g = hexdec(substr($hex,2,2));
        $b = hexdec(substr($hex,4,2));
    }
    $rgb = array($r, $g, $b);
    //return implode(",", $rgb); // returns the rgb values separated by commas
    return $rgb; // returns an array with the rgb values
}
    
13.11.2015 / 18:09
10

In JavaScript ♥

var rgb = ['001', '000', '255'];
var hex = '0100ff';

function converter(v) {
    if(typeof v === 'string') {
        var r = [];
    	v.match(/[0-9a-f]{2}/g).forEach(function(arr) {
        	r.push(('000' + parseInt(arr,16)).slice(-3));
        });
        return r;
    } else {
        var s;
    	v.forEach(function(arr) {
        	s = (s || '')  + ('00' + parseInt(arr,10).toString(16)).slice(-2);
        });
        return s;
    }
}

document.writeln('rgb: ' + converter(hex));
document.writeln('hex: ' + converter(rgb));

Reference from here

    
13.11.2015 / 18:23
8

In Python

def hex_to_rgb(value):
    value = value.lstrip('#')
    lv = len(value)
    return tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))

def rgb_to_hex(rgb):
    return '#%02x%02x%02x' % rgb
                                            # Resultados
print hex_to_rgb("#123456")                 # (18, 52, 86)
print hex_to_rgb("#ffffff")                 # (255, 255, 255)
print hex_to_rgb("#ffffffffffff")           # (65535, 65535, 65535)
print rgb_to_hex((18, 52, 86))              # #123456
print rgb_to_hex((255, 255, 255))           # #ffffff
print rgb_to_hex((65535, 65535, 65535))     # #ffffffffffff

See working at Ideone .

Source: Converting hex color to RGB and vice versa

    
13.11.2015 / 18:26
4

In C ++

// Este programa não converte entre as bases decimal e hexadecimal
// Apenas altera a representação dos inteiros

#include <iostream>
using namespace std;

int main(){
    unsigned int hexadecimal[3]={0xFF,0xFF,0xFF};
    unsigned int decimal[3]={255,255,255};

    for (auto i:hexadecimal)
        cout << i << " ";

    cout << endl;

    for (auto j:decimal)
        cout << hex << j << " ";
    return 0;

}
    
10.12.2015 / 19:11
0

In Java

Using the class Color of java.awt just do:

Color color = Color.decode("#FFFFFF");

That gets the color corresponding to the value in hexadecimal. Then you can get each RGB component of the resulting color through the methods:

  • getRed()
  • getGreen()
  • getBlue()

Example:

System.out.println(color.getRed()+" "+color.getGreen()+" "+color.getBlue()); //255 255 255

Font

Doing it manually looks more like the other answers:

String hex = "#FFFFFF";
int red =   Integer.parseInt(hex.substring(1 , 3), 16);
int green = Integer.parseInt(hex.substring(3 , 5), 16);
int blue =  Integer.parseInt(hex.substring(5 , 7), 16);
    
29.05.2018 / 13:39