Since jQuery always seems to return the color like rgb(r, g, b) for elements that have no alpha, you could simply use:
$(this).css('background-color').replace(')', ', 0.75)').replace('rgb', 'rgba');
Just make sure the background color isn't rgba already:
var bg = $(this).css('background-color');
if(bg.indexOf('a') == -1){
var result = bg.replace(')', ', 0.75)').replace('rgb', 'rgba');
}
Answer from Cerbrus on Stack Overflow Top answer 1 of 3
32
Since jQuery always seems to return the color like rgb(r, g, b) for elements that have no alpha, you could simply use:
$(this).css('background-color').replace(')', ', 0.75)').replace('rgb', 'rgba');
Just make sure the background color isn't rgba already:
var bg = $(this).css('background-color');
if(bg.indexOf('a') == -1){
var result = bg.replace(')', ', 0.75)').replace('rgb', 'rgba');
}
2 of 3
6
Another regex try http://jsfiddle.net/hc3BA/
var colour = 'rgb(123,123,123)',
new_col = colour.replace(/rgb/i, "rgba");
new_col = new_col.replace(/\)/i,',0.75)');
Top answer 1 of 16
238
//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)
*/
2 of 16
162
@ElDoRado1239 has the right idea, but there's also a cleaner way:
function hexToRGB(hex, alpha) {
var r = parseInt(hex.slice(1, 3), 16),
g = parseInt(hex.slice(3, 5), 16),
b = parseInt(hex.slice(5, 7), 16);
if (alpha) {
return "rgba(" + r + ", " + g + ", " + b + ", " + alpha + ")";
} else {
return "rgb(" + r + ", " + g + ", " + b + ")";
}
}
hexToRGB('#FF0000', 0.5);
GitHub
gist.github.com › danieliser › b4b24c9f772066bcf0a6
Convert Hex Color to rgba with opacity · GitHub
const convertHexToRGBA = (hex, opacity) => { const tempHex = hex.replace('#', ''); const r = parseInt(tempHex.substring(0, 2), 16); const g = parseInt(tempHex.substring(2, 4), 16); const b = parseInt(tempHex.substring(4, 6), 16); return ...
npm
npmjs.com › package › rgba-to-rgb
rgba-to-rgb - npm
October 24, 2018 - import rgbaToRgb from 'rgba-to-rgb' const RGB_background = 'rgb(255, 120, 5)' const RGBA_color = 'rgba(123, 233, 33, 0.5)' const RGB_color = rgbaToRgb(RGB_background, RGBA_color) // rgb(189, 177, 19) Based on rgba2rgb example by @marcodiiga · color · rgba ·
» npm install rgba-to-rgb
Published Oct 24, 2018
Version 1.0.2
Author iyegoroff
Repository https://github.com/iyegoroff/rgba-to-rgb
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › CSS › color_value › rgb
rgb() - CSS - MDN Web Docs
Note: For compatibility reasons, Web API-serialized color values are expressed as rgb() colors if the alpha channel value is exactly 1, and rgba() colors otherwise. In both cases, legacy syntax is used, with commas as separators (for example rgb(255, 0, 0)). Below are descriptions of the allowed values for both absolute and relative colors. ... Each value can be represented as a <number> between 0 and 255, a <percentage> between 0% and 100%, or the keyword none (equivalent to 0% in this case).
npm
npmjs.com › package › color-rgba
color-rgba - npm
September 15, 2023 - Convert color string to array with rgba channel values: "rgba(127,127,127,.1)" → [127,127,127,.1]. const rgba = require('color-rgba') rgba('red') // [255, 0, 0, 1] rgba('rgb(80, 120, 160)') // [80, 120, 160, 1] rgba('rgba(80, 120, 160, .5)') // [80, 120, 160, .5] rgba('hsla(109, 50%, 50%, .75)') // [87.125, 191.25, 63.75, .75] rgba`rgb(80 120 160 / 50%)` // [80, 120, 160, .5] Returns channels values as they are in the input color string argument.
» npm install color-rgba
Marcodiiga
marcodiiga.github.io › rgba-to-rgb-conversion
RGBA to RGB conversion
December 1, 2016 - How to convert a RGBA color likergba(0, 0, 0, 0.86)to a RGB hex value that takes the alpha component into account?
W3Schools
w3schools.com › html › html_colors_rgb.asp
HTML RGB and RGBA Colors
To display white, set all color parameters to 255, like this: rgb(255, 255, 255). ... RGBA color values are an extension of RGB color values with an Alpha channel - which specifies the opacity for a color.
GitHub
gist.github.com › RadGH › c277f220cb41cd0c222f297bad0bbbf5
Javascript convert RGB and RGBA to Hex, any format · GitHub
To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters ... var colors = []; // Test inputs: colors.push( "rgb\( 128, 255, 100 \);" ); colors.push( "rgb\( 200, 30, 5 \);" ); colors.push( "rgba\( 128, 255, 100, 0.25 \);" ); colors.push( "rgba\( 200, 30, 5, 0.5 \);" ); colors.push( "rgb\(128,255,100\);" ); colors.push( "rgb\(200, 30,5 \);" ); colors.push( "rgba\(128, 255, 100, 0.25\);" ); colors.push( "rgba\( 200,30,5, 0.5 \);" ); colors.push( "#ffcc00" ); colors.push( "#fc0" ); colors.push( "rgba\( 125 12 5 / 0.5 \);" ); co
Top answer 1 of 15
1
My version of hex2rbg:
- Accept short hex like #fff
- Algorithm compacity is o(n), should faster than using regex. e.g
String.replace, String.split, String.matchetc.. - Use constant space.
- Support rgb and rgba.
you may need remove hex.trim() if you are using IE8.
e.g.
hex2rgb('#fff') //rgb(255,255,255)
hex2rgb('#fff', 1) //rgba(255,255,255,1)
hex2rgb('#ffffff') //rgb(255,255,255)
hex2rgb('#ffffff', 1) //rgba(255,255,255,1)
code:
function hex2rgb (hex, opacity) {
hex = hex.trim();
hex = hex[0] === '#' ? hex.substr(1) : hex;
var bigint = parseInt(hex, 16), h = [];
if (hex.length === 3) {
h.push((bigint >> 4) & 255);
h.push((bigint >> 2) & 255);
} else {
h.push((bigint >> 16) & 255);
h.push((bigint >> 8) & 255);
}
h.push(bigint & 255);
if (arguments.length === 2) {
h.push(opacity);
return 'rgba('+h.join()+')';
} else {
return 'rgb('+h.join()+')';
}
}
2 of 15
1
This snippet converts hex to rgb and rgb to hex.
View demo
function hexToRgb(str) {
if ( /^#([0-9a-f]{3}|[0-9a-f]{6})$/ig.test(str) ) {
var hex = str.substr(1);
hex = hex.length == 3 ? hex.replace(/(.)/g, '$1$1') : hex;
var rgb = parseInt(hex, 16);
return 'rgb(' + [(rgb >> 16) & 255, (rgb >> 8) & 255, rgb & 255].join(',') + ')';
}
return false;
}
function rgbToHex(red, green, blue) {
var out = '#';
for (var i = 0; i < 3; ++i) {
var n = typeof arguments[i] == 'number' ? arguments[i] : parseInt(arguments[i]);
if (isNaN(n) || n < 0 || n > 255) {
return false;
}
out += (n < 16 ? '0' : '') + n.toString(16);
}
return out
}
GitHub
gist.github.com › tqc › 2564280
Convert RGBA to RGB · GitHub
Clone this repository at <script src="https://gist.github.com/tqc/2564280.js"></script> Save tqc/2564280 to your computer and use it in GitHub Desktop. Download ZIP · Convert RGBA to RGB · Raw · rgbatorgb.js · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below.
npm
npmjs.com › package › hex-to-rgba
hex-to-rgba - npm
July 15, 2019 - import hexToRgba from 'hex-to-rgba'; // Or if you're so inclined: // var hexToRgba = require("hex-to-rgba"); hexToRgba('112233'); // "rgba(17, 34, 51, 1)" hexToRgba('#112233'); // "rgba(17, 34, 51, 1)" hexToRgba('112233', '0.5'); // "rgba(17, ...
» npm install hex-to-rgba
Published Jul 15, 2019
Version 2.0.1
Author Just Thomas Hiorth Misund
Repository https://github.com/misund/hex-to-rgba
JSFiddle
jsfiddle.net › Mottie › xcqpF › 1 › light
rgb(a) to hex - JSFiddle - Code Playground
We do and so much of JSFiddle was still dependant on it till this day, but since almost all MooTools features are now available in native JS it was high-time to strip it out of the codebase.
GitHub
gist.github.com › junaidpv › 6119929
Javascript RGB(A) string conversion functions. · GitHub
Clone this repository at <script src="https://gist.github.com/junaidpv/6119929.js"></script> Save junaidpv/6119929 to your computer and use it in GitHub Desktop. Download ZIP · Javascript RGB(A) string conversion functions. Raw · rgba.js ·
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › CSS › Reference › Values › color_value › rgb
rgb() - CSS | MDN - Mozilla
Note: For compatibility reasons, Web API-serialized color values are expressed as rgb() colors if the alpha channel value is exactly 1, and rgba() colors otherwise. In both cases, legacy syntax is used, with commas as separators (for example rgb(255, 0, 0)). Below are descriptions of the allowed values for both absolute and relative colors. ... Each value can be represented as a <number> between 0 and 255, a <percentage> between 0% and 100%, or the keyword none (equivalent to 0% in this case).