Rgb to Hex Converter

RGB to hex and hex to RGB

Note: both versions of rgbToHex expect integer values for r, g and b, so you'll need to do your own rounding if you have non-integer values.

The following will do to the RGB to hex conversion and add any required zero padding:

function componentToHex(c) {
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}

function rgbToHex(r, g, b) {
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}

alert(rgbToHex(0, 51, 255)); // #0033ff

RGB to Hex converter

Just split the string up, and then use rgb:

x <- c("165 239 210", "111 45 93")
sapply(strsplit(x, " "), function(x)
rgb(x[1], x[2], x[3], maxColorValue=255))
#[1] "#A5EFD2" "#6F2D5D"

Convert RGB Color to HEX color

Solution:

Just use:

String hex = String.format("#%02x%02x%02x", redValue, greenValue, blueValue);

This will convert all the Red, Green and Blue values to Hex String.

Hope it helps.

converting RGB to HEX issue

Explanation:

It's because you're passing strings not numbers to supportFunc.

The result of match in prepareAndExecute is an array of strings, thus when you call toString in supportFunc, you are calling String.prototype.toString (not Number.prototype.toString) which return the string as is.

Solution:

In supportFunc, convert number to a number before using toString:

var hex = Number(number).toString(16);                       // using Number to convert a string to a number (you can use parseInt or unary + if you like)

or convert them before passing them to convert:

this.toHEX.convert(+numbers[0], +numbers[1], +numbers[2]);   // using unary +

Converting Hex to RGB value in Python

I believe that this does what you are looking for:

h = input('Enter hex: ').lstrip('#')
print('RGB =', tuple(int(h[i:i+2], 16) for i in (0, 2, 4)))

(The above was written for Python 3)

Sample run:

Enter hex: #B4FBB8
RGB = (180, 251, 184)

Writing to a file

To write to a file with handle fhandle while preserving the formatting:

fhandle.write('RGB = {}'.format( tuple(int(h[i:i+2], 16) for i in (0, 2, 4)) ))

How to convert vector of RGB values to Hex

You can split original vector with RGB values using strsplit function and then apply rgb2hex using do.call on a list produced by strsplit (output is a list).

# Input vector
RGB <- c("230-000-070", "255-000-000", "204-077-242")
# Function to apply
rgb2hex <- function(r, g, b) {rgb(r, g, b, maxColorValue = 255)}
# Split orignal vector and pass to rgb2hex with do.call as list
do.call(rgb2hex, strsplit(RGB, "-"))

Flutter: Convert RGB values to hex int

You can use the below function to convert RGB to Hex,

int hexOfRGBA(int r,int g,int b,{double opacity=1}) 
{
r = (r<0)?-r:r;
g = (g<0)?-g:g;
b = (b<0)?-b:b;
opacity = (opacity<0)?-opacity:opacity;
opacity = (opacity>1)?255:opacity*255;
r = (r>255)?255:r;
g = (g>255)?255:g;
b = (b>255)?255:b;
int a = opacity.toInt();
return int.parse('0x${a.toRadixString(16)}${r.toRadixString(16)}${g.toRadixString(16)}${b.toRadixString(16)}');
}

Usage:

    Color(hexOfRGBA(0,0,0,opacity: 0.7)); 

However for some reason if you want to keep your use-case specific,

You can use the below function to convert RGB to Hex (without
transparency),

int hexOfRGB(int r,int g,int b) 
{
r = (r<0)?-r:r;
g = (g<0)?-g:g;
b = (b<0)?-b:b;
r = (r>255)?255:r;
g = (g>255)?255:g;
b = (b>255)?255:b;
return int.parse('0xff${r.toRadixString(16)}${g.toRadixString(16)}${b.toRadixString(16)}');
}

Usage:

Color(hexOfRGB(255,255,255)); 

If you want to compulsorily include transparency (i.e. RGBA),

   int hexOfRGBA(int r,int g,int b,double opacity) 
{
r = (r<0)?-r:r;
g = (g<0)?-g:g;
b = (b<0)?-b:b;
opacity = (opacity<0)?-opacity:opacity;
opacity = (opacity>1)?255:opacity*255;
r = (r>255)?255:r;
g = (g>255)?255:g;
b = (b>255)?255:b;
int a = opacity.toInt();
return int.parse('0x${a.toRadixString(16)}${r.toRadixString(16)}${g.toRadixString(16)}${b.toRadixString(16)}');
}

Usage:

Color(hexOfRGBA(0,0,0,0.7)); 


Related Topics



Leave a reply



Submit