Convert 8-Digit Hex Colors to Rgba Colors

Convert 8-digit hex colors to rgba colors?

I have made a quick JSfiddle form that allows you to convert from 8-digit hex code into CSS rgba values ;)

https://jsfiddle.net/teddyrised/g02s07n4/embedded/result/

The basis is rather simple — to split the string you have provided into parts of 2-digits, and perform conversion into percentage ratios for the alpha channel, and to decimals for the RGB channels. The markup is as follow:

<form action="">
<select id="format">
<option value="rgba">RGBa: RRGGBBAA</option>
<option value="argb">aRGB: AARRGGBB</option>
</select>
<input type="text" id="hex" value="#949494E8" />
<button>Convert</button>
</form>
<p id="rgba"></p>

The logic:

// Remove hash
var hex = $('#hex').val().slice(1);

// Split to four channels
var c = hex.match(/.{1,2}/g);

// Function: to decimals (for RGB)
var d = function(v) {
return parseInt(v, 16);
};
// Function: to percentage (for alpha), to 3 decimals
var p = function(v) {
return parseFloat(parseInt((parseInt(v, 16)/255)*1000)/1000);
};

// Check format: if it's argb, pop the alpha value from the end and move it to front
var a, rgb=[];
if($('#format').val() === 'argb') {
c.push(c.shift());
}

// Convert array into rgba values
a = p(c[3]);
$.each(c.slice(0,3), function(i,v) {
rgb.push(d(v));
});

The gist of conversion is as follow:

  • Converting the RGB channels in hex, into decimal values. This is done by using parseInt(hexValue, 16).
  • Converting the alpha channel in hex, into percentage ratio. This is done by simply converting it to decimal values (see above point), and calculating its relative value to 255.

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

8 digit hex rgba value

Black has no red, green or blue, but it is not transparent, so the alpha needs to be ff. Therefore the combination you seek is

000000ff

Convert Hex to RGBA

//If you write your own code, remember hex color shortcuts (eg., #fff, #000)

function hexToRgbA(hex){
var c;
if(/^#([A-Fa-f0-9]{3}){1,2}$/.test(hex)){
c= hex.substring(1).split('');
if(c.length== 3){
c= [c[0], c[0], c[1], c[1], c[2], c[2]];
}
c= '0x'+c.join('');
return 'rgba('+[(c>>16)&255, (c>>8)&255, c&255].join(',')+',1)';
}
throw new Error('Bad Hex');
}

hexToRgbA('#fbafff')

/* returned value: (String)
rgba(251,175,255,1)
*/

How to give opacity to a color with hex value in css?

You may use this way to convert the transparent for your current hex colour

For example, you want to set 40% alpha transparency to #000000 (black colour), you need to add 66 like this #66000000. The format is #AARRGGBB so you could use a format like #1C00ff00.

You could also check the full table hex -> transparent colour here
https://gist.github.com/lopspower/03fb1cc0ac9f32ef38f4

How do I convert a UIColor to a 3/4/6/8 digits hexadecimal string in Swift?

Here's an extension for UIColor that can provide hexStrings in many formats including 3, 4, 6, and 8 digit forms:

extension UIColor {
enum HexFormat {
case RGB
case ARGB
case RGBA
case RRGGBB
case AARRGGBB
case RRGGBBAA
}

enum HexDigits {
case d3, d4, d6, d8
}

func hexString(_ format: HexFormat = .RRGGBBAA) -> String {
let maxi = [.RGB, .ARGB, .RGBA].contains(format) ? 16 : 256

func toI(_ f: CGFloat) -> Int {
return min(maxi - 1, Int(CGFloat(maxi) * f))
}

var r: CGFloat = 0
var g: CGFloat = 0
var b: CGFloat = 0
var a: CGFloat = 0

self.getRed(&r, green: &g, blue: &b, alpha: &a)

let ri = toI(r)
let gi = toI(g)
let bi = toI(b)
let ai = toI(a)

switch format {
case .RGB: return String(format: "#%X%X%X", ri, gi, bi)
case .ARGB: return String(format: "#%X%X%X%X", ai, ri, gi, bi)
case .RGBA: return String(format: "#%X%X%X%X", ri, gi, bi, ai)
case .RRGGBB: return String(format: "#%02X%02X%02X", ri, gi, bi)
case .AARRGGBB: return String(format: "#%02X%02X%02X%02X", ai, ri, gi, bi)
case .RRGGBBAA: return String(format: "#%02X%02X%02X%02X", ri, gi, bi, ai)
}
}

func hexString(_ digits: HexDigits) -> String {
switch digits {
case .d3: return hexString(.RGB)
case .d4: return hexString(.RGBA)
case .d6: return hexString(.RRGGBB)
case .d8: return hexString(.RRGGBBAA)
}
}
}

Examples

print(UIColor.red.hexString(.d3))  // #F00
print(UIColor.red.hexString(.d4)) // #F00F
print(UIColor.red.hexString(.d6)) // #FF0000
print(UIColor.red.hexString(.d8)) // #FF0000FF

print(UIColor.green.hexString(.RGB)) // #0F0
print(UIColor.green.hexString(.ARGB)) // #F0F0
print(UIColor.green.hexString(.RGBA)) // #0F0F
print(UIColor.green.hexString(.RRGGBB)) // #00FF00
print(UIColor.green.hexString(.AARRGGBB)) // #FF00FF00
print(UIColor.green.hexString(.RRGGBBAA)) // #00FF00FF

print(UIColor(red: 0.25, green: 0.5, blue: 0.75, alpha: 0.3333).hexString()) // #4080c055


Related Topics



Leave a reply



Submit