Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

spread

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);
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: js object filter by keys 
Javascript :: js foreach syntax 
Javascript :: disable input field javascript 
Javascript :: filter 
Javascript :: password 
Javascript :: export default function react 
Javascript :: javascript basic programs 
Javascript :: javascript interview questions 
Javascript :: convert html to javascript 
Javascript :: js function arguments 
Javascript :: vuejs accessing props from data 
Javascript :: vue js laravel tutorial 
Javascript :: how to do subtraction in javascript 
Javascript :: what is diffrence between redux and context 
Javascript :: javascript document get by attribute 
Javascript :: js date minus 18 years 
Javascript :: how to get the last element of an array in javascript 
Javascript :: preview file before upload in react 
Javascript :: how to use break in javascript 
Javascript :: javascript print square 
Javascript :: Adding an item to an array 
Javascript :: default function parameters javascript 
Javascript :: js parse bool 
Javascript :: change string with string js 
Javascript :: how to declare objects inside arrays in javascript 
Javascript :: how to build tree array from flat array in javascript 
Javascript :: solid in css 
Javascript :: Dynamically load JS inside JS 
Javascript :: apoolo uselaxyQuery bypass cache 
Javascript :: expression javascript 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =