How to Get the Background Color Code of an Element in Hex

How to get the background color code of an element in hex?

Check example link below and click on the div to get the color value in hex.

var color = '';$('div').click(function() {  var x = $(this).css('backgroundColor');  hexc(x);  console.log(color);})
function hexc(colorval) { var parts = colorval.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/); delete(parts[0]); for (var i = 1; i <= 3; ++i) { parts[i] = parseInt(parts[i]).toString(16); if (parts[i].length == 1) parts[i] = '0' + parts[i]; } color = '#' + parts.join('');}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><div class='div' style='background-color: #f5b405'>Click me!</div>

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)

Get color of DIV in hex format JQuery

I know, not the answer to your question, but have you considered jQuery's toggleClass() option?

Define a highlighted CSS class:

DIV.default { background-color: whitesmoke; }
DIV.highlighted { background-color: yellow; }

and then when the user clicks your DIV:

function getTraits(trait) {
$("#"+trait).on("click", function() {
// Toggle both classes so as to turn one "off"
// and the other "on"
$(this).toggleClass('default');
$(this).toggleClass('highlighted');

// Ensure we have at least one class (default)
var hasOne = $(this).hasClass('highlighted') || $(this).hasClass('default');
if (!hasOne) {
$(this).addClass('default');
}
})
}

How can I get the background colour from a style attribute as a hex value using JavaScript in IE 9?

You can use the following to convert it (source).

I believe most modern browsers (including chrome) return the rgb and not the hex.

Perhaps you could do a basic match to decide if it was hex or rgb, and then convert the rgb if required.

function rgb2hex(rgb) {
rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
function hex(x) {
return ("0" + parseInt(x).toString(16)).slice(-2);
}
return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}

Updated version (supports alpha), as found by Cody O'Dell:

//Function to convert hex format to a rgb color
function rgb2hex(rgb){
rgb = rgb.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i);
return (rgb && rgb.length === 4) ? "#" +
("0" + parseInt(rgb[1],10).toString(16)).slice(-2) +
("0" + parseInt(rgb[2],10).toString(16)).slice(-2) +
("0" + parseInt(rgb[3],10).toString(16)).slice(-2) : '';
}

Get Background Color of a View in hex

LinearLayout layout = (LinearLayout) findViewById(R.id.lay1);
ColorDrawable viewColor = (ColorDrawable) layout.getBackground();
int colorId = viewColor.getColor();

After getting as integer type of color, now you have to convert to hexadecimal:

String hexColor = String.format("#%06X", (0xFFFFFF & colorId));

Hope this helps..

How to get the background color of an element (set to RGBA) in Javascript?

I will consider this answer where I am detailing how the color is calculated between two layers to write the following script.

/* Utility function that you can easily find in the net */
function extractRgb (css) {
return css.match(/[0-9.]+/gi)
}
function rgbToHex(r, g, b) {
return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}
/**/

var c = window.getComputedStyle(document.getElementById('box')).backgroundColor;
c = extractRgb(c);

for(var i=0;i<3;i++)
c[i] = parseInt(c[i]*c[3] + 255*(1 - c[3])); /* Check the linked answer for details */

console.log(rgbToHex(c[0],c[1],c[2]) )
#box {
width: 100px;
height: 100px;
margin: 10px;
background-color: rgba(0, 0, 0, 0.3);
}
<div id="box"></div>

How to get background-color in hex format from element using Robot Framework

You could create a keyword in python that transform colors from RGB to HEX.

Let's say you create a module named testLibrary.py in where you will create the python method that change the format of colors:

def transform_RGB_to_HEX(arg1):
hex_result = "".join([format(val, '02X') for val in arg1])
return hex_result

The only thing you need to do now is import the test library in your robot file and call the keyword as you would do with an already existing one.

*** Settings ***
| Library | MyLibrary.py

*** Test Cases ***
| Example that get color in format RGB and transform it into HEX
| ${elements} | Get Webelement | (//div[@class="slds-col call-scripticon"])[1]
| ${bg color} | Call Method | ${elements} | value_of_css_property | background-color
| ${hex_color} | transform RGB to HEX | ${bg color}



Related Topics



Leave a reply



Submit