-
Notifications
You must be signed in to change notification settings - Fork 127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add warning for low contrast and equal colors #86
Changes from 11 commits
819d68d
10bfa6b
323ad34
1b53467
9d6e4cb
da16700
de319c2
9dae10d
7627beb
39cc907
f0009f5
496edca
33a661a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1372,6 +1372,110 @@ const RandomTips = (function () {// eslint-disable-line no-unused-vars | |
return me; | ||
})(); | ||
|
||
const Colors = (() => { // eslint-disable-line no-unused-vars | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, this module looks great! So for doing #20 later, everything is set. |
||
const me = {}; | ||
|
||
me.ContrastBreakpoints = { | ||
A: 2, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No my numbers were not all the A/AA/AAA things… But I'll change that by myself. |
||
AA: 3, | ||
AAA: 4.5, | ||
}; | ||
|
||
/** | ||
* Calculates the contrast between two colors | ||
* | ||
* @name Colors.contrastRatio | ||
* @function | ||
* @param {Array} rgb1 | ||
* @param {Array} rgb2 | ||
* @returns {int} | ||
*/ | ||
me.contrastRatio = function(rgb1, rgb2) { | ||
const l1 = luminance(rgb1); | ||
const l2 = luminance(rgb2); | ||
// Formula: https://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef | ||
return (Math.max(l1, l2) + 0.05) / (Math.min(l1, l2) + 0.05); | ||
}; | ||
|
||
/** | ||
* Calculates the luminance of a given RGB color. | ||
* | ||
* @name Colors.luminance | ||
* @function | ||
* @private | ||
* @param {Array} rgb | ||
* @returns {Array|null} | ||
*/ | ||
function luminance(rgb) { | ||
// Formula: https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef | ||
let r = rgb[0] / 255; | ||
let g = rgb[1] / 255; | ||
let b = rgb[2] / 255; | ||
// Note: I'm using 0.04045 here (instead of 0.03928) because 0.04045 is the | ||
// number used in the actual sRGB standard. | ||
// See also: https://github.com/w3c/wcag21/issues/815 | ||
r = r < 0.04045 ? r / 12.92 : Math.pow((r + 0.055) / 1.055, 2.4); | ||
g = g < 0.04045 ? g / 12.92 : Math.pow((g + 0.055) / 1.055, 2.4); | ||
b = b < 0.04045 ? b / 12.92 : Math.pow((b + 0.055) / 1.055, 2.4); | ||
return 0.2126 * r + 0.7152 * g + 0.0722 * b; | ||
} | ||
|
||
/** | ||
* Returns the complementary color of a given RGB array. | ||
* | ||
* @name Colors.invertColor | ||
* @function | ||
* @param {Array} rgb | ||
* @returns {string} | ||
*/ | ||
me.invertColor = function(rgb) { | ||
// invert color components | ||
const r = (255 - rgb[0]).toString(16); | ||
const g = (255 - rgb[1]).toString(16); | ||
const b = (255 - rgb[2]).toString(16); | ||
// pad each with zeros and return | ||
return `#${padZero(r)}${padZero(g)}${padZero(b)}`; | ||
}; | ||
|
||
/** | ||
* Adds missing zeros in front of a string. | ||
* | ||
* @name Colors.padZero | ||
* @function | ||
* @private | ||
* @param {string} string | ||
* @param {int} length | ||
* @returns {string} | ||
*/ | ||
function padZero(string, length) { | ||
length = length || 2; | ||
const zeros = new Array(length).join("0"); | ||
return (zeros + string).slice(-length); | ||
} | ||
|
||
/** | ||
* Converts a hex color string to RGB. | ||
* | ||
* @name Colors.hexToRgb | ||
* @function | ||
* @param {string} hex | ||
* @returns {Array|null} | ||
*/ | ||
me.hexToRgb = function(hex) { | ||
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); | ||
if (result) { | ||
return [ | ||
parseInt(result[1], 16), | ||
parseInt(result[2], 16), | ||
parseInt(result[3], 16) | ||
]; | ||
} | ||
return null; | ||
}; | ||
|
||
return me; | ||
})(); | ||
|
||
// init modules | ||
AddonSettings.loadOptions(); | ||
Logger.init(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MAybe you can try to shorten this a bit?
Instead of calculate maybe just "Use". (That the JS has to calculate this is a technical detail the user does not even want to know. 😄)
And instead of contrast maybe contrasting color? Or something else like "best color"? Remember it does not matter to be technically 100% accurate here, the user should just know what happens when they click the button.
Theoretically it could e.g. also be okay for the user to get some color variation (i.e. not 100%ly the contrast color, but a color, which has enough contrast, so the message goes away.)