How to Extract Color Values from Rgb String in JavaScript

How to extract color values from rgb string in javascript

You can use a regex:

function getRGB(str){  var match = str.match(/rgba?\((\d{1,3}), ?(\d{1,3}), ?(\d{1,3})\)?(?:, ?(\d(?:\.\d?))\))?/);  return match ? {    red: match[1],    green: match[2],    blue: match[3]  } : {};}
console.log(getRGB("rgb(211, 211, 211)"));console.log(getRGB("rgba(211, 0, 211, 0.5)"));

Get a color component from an rgb string in Javascript?

NOTE - We're all on board with the regex ate my brains and kicked my dog attitude, but the regex version just seems the better method. My opinion. Check it out.

Non-regex method:

var rgb = 'rgb(200, 12, 53)';

rgb = rgb.substring(4, rgb.length-1)
.replace(/ /g, '')
.split(',');

console.log(rgb);

http://jsfiddle.net/userdude/Fg9Ba/

Outputs:

["200", "12", "53"]

Or... A really simple regex:

EDIT: Ooops, had an i in the regex for some reason.

var rgb = 'rgb(200, 12, 53)';

rgb = rgb.replace(/[^\d,]/g, '').split(',');

console.log(rgb);

http://jsfiddle.net/userdude/Fg9Ba/2

Extracting the RGB value from a string

You can use a regex to get the string between two strings:

let string = "<span style=\"background-color: rgb(230, 0, 0);\">"

let pattern = "(?<=background-color: )(.*)(?=;)"
if let rgb = string.range(of: pattern, options: .regularExpression).map({String(string[$0])}) {
print(rgb) // "rgb(230, 0, 0)"
}

How to extract r, g, b, a values from CSS color?

var c = $('body').css('background-color');
var rgb = c.replace(/^(rgb|rgba)\(/,'').replace(/\)$/,'').replace(/\s/g,'').split(',');

for(var i in rgb) {
console.log(rgb[i]);
}

Try it here http://jsbin.com/uhawa4

Edit :

var c = $('body').css('background-color');
var rgb = c.replace(/^rgba?\(|\s+|\)$/g,'').split(',');

for(var i in rgb) {
console.log(rgb[i]);
}

or even simpler way, just aiming at numbers

var c = 'rgba(60,4,2,6)';
var rgb = c.match(/\d+/g);

for(var i in rgb) {
console.log(rgb[i]);
}

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

Regex to extract RGB(r,g,b) from RGBA(r,g,b,a)

You could also write a more consolidated version of the regex like this:

var str = 'rgba(14, 48, 71, 0.3)';/* Goal: rgb(14,48,71) */
var rgx = /^rgba\(((,?\s*\d+){3}).+$/
console.log (str.replace(rgx, 'rgb($1)'));

How can i get the red green and blue values from an rgb/rgba string?

I'd avoid regex for a predictable string, and suggest:

// assigning the rgb() colour to a variable:
var colorString = "rgba(111,222,333,0.5)",

// using String.prototype.substring() to retrieve
// the substring between the indices of the opening
// and closing parentheses:
colorsOnly = colorString.substring(
// here we find the index of the opening
// parenthesis, and add 1 to that index
// so that the substring starts after that
// parenthesis:
colorString.indexOf('(') + 1,

// and terminating the substring at the
// index of the closing parenthesis:
colorString.lastIndexOf(')')
// here we split that substring on occurrence
// of a comma followed by zero or more white-
// space characters:
).split(/,\s*/),

// String.prototype.split() returns an Array,
// here we assign those Array-elements to the
// various colour-, or opacity-, variables:
red = colorsOnly[0],
green = colorsOnly[1],
blue = colorsOnly[2],
opacity = colorsOnly[3];

How does one extract the color-stop values from a string which contains the definition of a linear gradient?

You can use String#match.

let str = "linear-gradient(90deg, rgba(243,231,231,1), rgba(12,0,0,0.48))";
let res = str.match(/rgba\(.*?\)/g);
console.log(res);

How to get hex color value rather than RGB value?

var hexDigits = new Array
("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");

//Function to convert rgb color to hex format
function rgb2hex(rgb) {
rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}

function hex(x) {
return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
}

(Source)

How to extract two rgb values/strings from the another string using regex or another method

You can use this:

string.replace(/^.*?linear-gradient *\((.+)/, function($1, $2) {
return $1.match(/rgb *\([^)]+\)/g); } );
//=> rgb(100, 106, 237),rgb(101, 222, 108)

Assuming there is no other rgb segment outside closing bracket of linear-gradient



Related Topics



Leave a reply



Submit