Your js example is not valid and should not do anything ...
You need to set the style on each span element;
Copyvar ea = document.getElementsByClassName('name');
for(var i = 0; i < ea.length; i++)
ea[i].style.backgroundColor = "yellow";
Answer from Alex K. on Stack OverflowYour js example is not valid and should not do anything ...
You need to set the style on each span element;
Copyvar ea = document.getElementsByClassName('name');
for(var i = 0; i < ea.length; i++)
ea[i].style.backgroundColor = "yellow";
Tried with jQuery, http://jsfiddle.net/sameerast/gT9eh/
Javascript code for text color and background color
javascript - Change text color based on the background color - Stack Overflow
javascript - Change text color based on brightness of the covered background area? - Stack Overflow
javascript - Determine the best text color for a given background color - User Experience Stack Exchange
Probably you need this:
Change text color based on brightness of the covered background area?
var rgb = [255, 0, 0];
setInterval(function() {
var c = 'rgb(' + rgb[0] + ',' + rgb[1] + ',' + rgb[2] + ')';
var o = Math.round(((parseInt(rgb[0]) * 299) + (parseInt(rgb[1]) * 587) + (parseInt(rgb[2]) * 114)) / 1000);
(o > 125) ? $('#bg').css('color', 'black'): $('#bg').css('color', 'white'); //http://www.w3.org/TR/AERT#color-contrast
$('#bg').css('background-color', c);
rgb[0] = Math.round(Math.random() * 255);
rgb[1] = Math.round(Math.random() * 255);
rgb[2] = Math.round(Math.random() * 255);
}, 1000);
JSFiddle
Updated:
Use this example on JSFiddle, just change background color
I do realize you are looking for a JS solution. However, if anyone wants a css solution here is a solution http://codepen.io/hellouniverse/pen/JNwvyL
$bg-color: black; // This can change
$white : #fff;
$black : #000;
@function set-text-color($color) {
@if (lightness( $color ) > 50) {
@return $black; // Lighter color, return black
}
@else {
@return $white; // Darker color, return white
}
}
.text--color {
background: $bg-color;
color: set-text-color($bg-color);
}
// this is here just for demon purpose
.text {
padding: 10px;
text-align: center;
}
<p class="text text--color">I change in color</p>
Interesting resources for this:
- W3C - Ensure that foreground and background color combinations provide sufficient contrast
- Calculating the Perceived Brightness of a Color
Here's the W3C algorithm (with JSFiddle demo too):
const rgb = [255, 0, 0];
// Randomly change to showcase updates
setInterval(setContrast, 1000);
function setContrast() {
// Randomly update colours
rgb[0] = Math.round(Math.random() * 255);
rgb[1] = Math.round(Math.random() * 255);
rgb[2] = Math.round(Math.random() * 255);
// http://www.w3.org/TR/AERT#color-contrast
const brightness = Math.round(((parseInt(rgb[0]) * 299) +
(parseInt(rgb[1]) * 587) +
(parseInt(rgb[2]) * 114)) / 1000);
const textColour = (brightness > 125) ? 'black' : 'white';
const backgroundColour = 'rgb(' + rgb[0] + ',' + rgb[1] + ',' + rgb[2] + ')';
$('#bg').css('color', textColour);
$('#bg').css('background-color', backgroundColour);
}
#bg {
width: 200px;
height: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="bg">Text Example</div>
This article on 24 ways about Calculating Color Contrast might be of interest to you. Ignore the first set of functions because they're wrong, but the YIQ formula will help you determine whether or not to use a light or dark foreground color.
Once you obtain the element's (or ancestor's) background color, you can use this function from the article to determine a suitable foreground color:
function getContrastYIQ(hexcolor){
var r = parseInt(hexcolor.substring(1,3),16);
var g = parseInt(hexcolor.substring(3,5),16);
var b = parseInt(hexcolor.substring(5,7),16);
var yiq = ((r*299)+(g*587)+(b*114))/1000;
return (yiq >= 128) ? 'black' : 'white';
}
Don't get fooled into thinking that you need to find the best shade of grey to use.
Given any background color, the foreground color that provides the highest contrast ratio will be either white or black, and not some shade of grey in between.
Having said that, pure black or white can seem a little harsh and less pleasing to the eyes - too much contrast can be quite distracting for some users. So, assuming for the purposes of this answer that black means off-black (eg #101010), and white means off-white (eg #f0f0f0), the question then becomes "is black or white better for my background color"?
In another answer I go into more detail with some good example screenshots of how contrast ratio varies for ranges of backgrounds.
In summary, for any two given colors there is a formula for contrast ratio:
The contrast ratio is calculated as (L1 + 0.05) / (L2 + 0.05), where
L1 is the: relative luminance of the lighter of the colors, and
L2 is the relative luminance of the darker of the colors.
In your case you simply need to calculate the contrast ratio for white and your bg color; and again for black and your bg color, and then pick black or white depending on the highest contrast ratio.
'Pleasing to the eyes' is very subjective.
But there are a lot of contrast tools like http://leaverou.github.io/contrast-ratio/ and https://contrastchecker.com which take in to account brightness contrast as well as hue contrast. Many of these tools are based on WCAG guidelines which makes them fairly standardized in their outcomes, and therefore widely used and accepted.
Pass the class name to the function and use it
function changeForeground(cls){
var div = document.getElementById("foreground").className=cls;
}
function changeBackground(cls){
var div = document.getElementById("background").className=cls;
}
.colorA{
color: #4581cf;
}
.colorB{
color: #B7E2FF;
}
.backgroundA{
background-color: #4581cf;
}
.backgroundB{
background-color: #B7E2FF;
}
<div id="background" class="backgroundA">
<div id="foreground" class="colorB">
<p>blah blah blah</p>
</div>
</div>
Foreground
<INPUT type="button" Value="A" class="colorA" id="button1" onclick ="changeForeground('colorA')";>
<INPUT type="button" Value="B" class="colorB" id="button2" onclick ="changeForeground('colorB')";>
Background
<INPUT type="button" Value="C" class="backgroundA" id="button3" onclick ="changeBackground('backgroundA')";>
<INPUT type="button" Value="D" class="backgroundB" id="button4" onclick ="changeBackground('backgroundB')";>
Try this:
function changeForeground()
{
var div = document.getElementById("foreground").style.color = "#4581cf";
}
function changeBackground()
{
var div = document.getElementById("background").style.backgroundColor = "#B7E2FF";
}
I'm working on a landing page with a scrollspy navigation bar on the left. I want the scrollspy's color and text colors to change dynamically based on the background color.
For example, if the section has a dark background, I want the scrollspy to have a light color (and vice versa). I'm stuck on how to detect the background color of sections and adjust the scrollspy's color accordingly.
Please provide me a solution for this!!!!!!
I have edit the details for this question on stackoverflow link
https://stackoverflow.com/q/78987758/22625136