You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

16 lines
511 B

2 years ago
2 years ago
  1. import Vue from 'vue'
  2. declare module 'vue/types/vue' {
  3. interface Vue {
  4. $contrastColor(hexString: string): string
  5. }
  6. }
  7. Vue.prototype.$contrastColor = (hexString: string) => {
  8. // W3c offers a formula for calculating ideal color:
  9. // https://www.w3.org/TR/AERT/#color-contrast
  10. const r = parseInt(hexString.substr(1, 2), 16)
  11. const g = parseInt(hexString.substr(3, 2), 16)
  12. const b = parseInt(hexString.substr(5, 2), 16)
  13. return (r * 299 + g * 587 + b * 114) / 1000 < 128 ? '#ffffff' : '#000000'
  14. }