Skip to content

Commit

Permalink
Merge pull request #4958 from nextcloud-libraries/enh/make-contrast-c…
Browse files Browse the repository at this point in the history
…lear-ARIA-CHANGE

enh(aria): changed check color to adhere to 3:1 ratio
  • Loading branch information
emoral435 committed Dec 15, 2023
2 parents 78f5179 + 938b6a2 commit 3288f79
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/components/NcColorPicker/NcColorPicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export default {
:style="{ backgroundColor: color }"
class="color-picker__simple-color-circle"
:class="{ 'color-picker__simple-color-circle--active' : color === currentColor }">
<Check v-if="color === currentColor" :size="20" />
<Check v-if="color === currentColor" :size="20" :fill-color="contrastColor" />
<input type="radio"
class="hidden-visually"
:aria-label="name"
Expand Down Expand Up @@ -323,6 +323,11 @@ export default {
uid() {
return GenRandomId()
},
contrastColor() {
const black = '#000000'
const white = '#FFFFFF'
return (this.calculateLuma(this.currentColor) > 0.5) ? black : white
},
},
watch: {
Expand Down Expand Up @@ -386,6 +391,28 @@ export default {
this.$emit('input', color)
},
/**
* Calculate luminance of provided hex color
*
* @param {string} color the hex color
*/
calculateLuma(color) {
const [red, green, blue] = this.hexToRGB(color)
return (0.2126 * red + 0.7152 * green + 0.0722 * blue) / 255
},
/**
* Convert hex color to RGB
*
* @param {string} hex the hex color
*/
hexToRGB(hex) {
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex)
return result
? [parseInt(result[1], 16), parseInt(result[2], 16), parseInt(result[3], 16)]
: null
},
},
}
Expand Down

0 comments on commit 3288f79

Please sign in to comment.