Skip to content

Instantly share code, notes, and snippets.

BenOsodrac

Block or report user

Report or block BenOsodrac

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@BenOsodrac
BenOsodrac / getBrightness()
Created Apr 13, 2019
getBrightness color function
View getBrightness()
var getBrightness = function(hex) {
var rgb = 'rgb(' + (hex = hex.replace('#', '')).match(new RegExp('(.{' + hex.length/3 + '})', 'g')).map(function(l) { return parseInt(hex.length%2 ? l+l : l, 16); }).join(',') + ')';
// Get array of RGB values
rgb = rgb.replace(/[^\d,]/g, '').split(',');
var r = rgb[0], g = rgb[1], b = rgb[2];
var brightness = Math.floor((r * 299 + g * 587 + b * 114) / 1000);
@BenOsodrac
BenOsodrac / getContrast
Last active Apr 14, 2019
getContrast color function
View getContrast
var getContrast = function(background, foreground) {
// Convert hex to rgb
var backgroundRGB = 'rgb(' + (background = background.replace('#', '')).match(new RegExp('(.{' + background.length/3 + '})', 'g')).map(function(l) { return parseInt(background.length%2 ? l+l : l, 16); }).join(',') + ')';
var foregroundRGB = 'rgb(' + (foreground = foreground.replace('#', '')).match(new RegExp('(.{' + foreground.length/3 + '})', 'g')).map(function(l) { return parseInt(foreground.length%2 ? l+l : l, 16); }).join(',') + ')';
// Get array of RGB values
var backgroundRGBArray = backgroundRGB.replace(/[^\d,]/g, '').split(',');
var foregroundRGBArray = foregroundRGB.replace(/[^\d,]/g, '').split(',');
You can’t perform that action at this time.