Convert Rgba to Hex

RGBA and RGB to Hex

You could make it a wrapper,and call the function based on the arguments passed

 //takes both RGB and RGBA and convert to HEX like  #000000
// input will be string like this => rgb(0,0,0) or rgba(255,255,255, 0.5)
function anyToHex(r, g, b, a) {
if(a === undefined){
return rgb2hex(r, g, b);
}
return rgba2hex(r, g, b, a);
}

Getting Hex from RGBA in js

If you don't need the "a" from rgbA, tell me, I will remove its support.

But basically, you just convert every integer to hex string and add 0 if needed.

function rgbaToHex (r,g,b,a) {
var outParts = [
r.toString(16),
g.toString(16),
b.toString(16),
Math.round(a * 255).toString(16).substring(0, 2)
];

// Pad single-digit output values
outParts.forEach(function (part, i) {
if (part.length === 1) {
outParts[i] = '0' + part;
}
})

return ('#' + outParts.join(''));
}

alert(rgbaToHex(255,34,56,1));

R function for RGBA to HEX color conversion

This topic has been discussed Convert RGBA to HEX
however, I am not aware of an R package, that offers a ready to use function.

You can convert RGBA to RGB:

Since this depends on the background pixel's color (Convert RGBA color to RGB) you have to define the background color.
color_RGBA is your RGBA color and background_RGB is the background color.
You can take col2rgb("white") as background_RGB for example.

rgba2rgb <- function(background_RGB, color_RGBA){

# get alpha
alpha=color_RGBA[4]

# get new color
new_col=matrix(c(
(1 - alpha) * background_RGB[1] + alpha * color_RGBA[1],
(1 - alpha) * background_RGB[2] + alpha * color_RGBA[2],
(1 - alpha) * background_RGB[3] + alpha * color_RGBA[3]),
nrow=3,ncol=1,dimnames=list(c("red","green","blue"))
)
return(new_col)
}

and then convert RGB to HEX:

rgb2hex <- function(x) rgb(x[1], x[2], x[3], maxColorValue = 255)

How to convert RGBA colors (with opacity) to hex/solid colors?

try to extract "r, g, b, and a" from your color in rgba color coding. Then, you can use the following function to convert it to hex color coding. Also, if opacity = false, you can change a = 1 so that you can again use the same function.

function RGBAToHexA(r,g,b,a) {
r = r.toString(16);
g = g.toString(16);
b = b.toString(16);
a = Math.round(a * 255).toString(16);

if (r.length == 1)
r = "0" + r;
if (g.length == 1)
g = "0" + g;
if (b.length == 1)
b = "0" + b;
if (a.length == 1)
a = "0" + a;

return "#" + r + g + b + a;
}

Convert RGBA values to hex color code

The Color parameters must be floats between 1f and 0f. So this is a valid color:

int color = toHex(new Color(1f, 1f, 1f, 1f));

Which is white.

How to convert RGBA color codes to 8 Digit Hex in PHP?

You can use the printf() family of functions to convert to a properly padded hex string. Decimals cannot be represented in hex, so the value is taken as a fraction of 0xFF.

$rgba = "rgba(123,100,23,.5)";

// get the values
preg_match_all("/([\\d.]+)/", $rgba, $matches);

// output
$hex = sprintf(
"&H%02X%02X%02X%02X",
$matches[1][2], // blue
$matches[1][1], // green
$matches[1][0], // red
$matches[1][3] * 255, // adjusted opacity
);
echo $hex;

Output:

&H17647B7F


Related Topics



Leave a reply



Submit