55 <h2 >{{ color }}</h2 >
66 <span class =" flex space-x-2" >
77 <span
8- v-for =" value of Object.keys(colors[color])"
8+ v-for =" value of keys(colors[color])"
9+ :id =" `${color}-${value}`"
910 :key =" value"
1011 class =" color-box flex-grow py-6 md:py-8 rounded cursor-pointer"
11- :class =" value === 'DEFAULT' ? `bg-${color}` : `bg-${color}-${value}`"
12+ :class =" [ value === 'DEFAULT' ? `bg-${color}` : `bg-${color}-${value}`, color === 'white' ? 'border' : ''] "
1213 @click =" select(color, value)"
1314 />
1415 </span >
@@ -44,8 +45,6 @@ export default {
4445 layout: ' tw' ,
4546 data () {
4647 // Remove unecessary colors
47- delete theme .colors .white
48- delete theme .colors .black
4948 delete theme .colors .current
5049 delete theme .colors .transparent
5150
@@ -61,14 +60,19 @@ export default {
6160 return ' bg-white border-2 border-stone text-stone'
6261 }
6362 const textColor =
64- this .selected .class .includes (' dark' ) ||
65- [' 500' , ' 600' , ' 700' , ' 800' , ' 900' ].includes (this .selected .value )
63+ this .selected .type === ' dark'
6664 ? ' text_white'
6765 : ' text_black bg_black'
6866 return ` border-${ this .selected .class } ${ textColor} `
6967 }
7068 },
7169 methods: {
70+ keys (color ) {
71+ if (typeof color === ' string' ) {
72+ return [' DEFAULT' ]
73+ }
74+ return Object .keys (color)
75+ },
7276 select (color , value ) {
7377 if (
7478 this .selected &&
@@ -78,15 +82,38 @@ export default {
7882 this .selected = null
7983 return
8084 }
85+ let colorValue = this .colors [color]
86+ if (typeof colorValue !== ' string' ) {
87+ colorValue = colorValue[value]
88+ }
8189 const twClass = value === ' DEFAULT' ? ` ${ color} ` : ` ${ color} -${ value} `
8290 this .selected = {
8391 color,
8492 value,
93+ type: this .lightOrDark (color, value),
8594 class: twClass,
8695 bgClass: ` bg-${ twClass} ` ,
8796 textClass: ` text-${ twClass} `
8897 }
8998 },
99+ lightOrDark (color , value ) {
100+ const el = document .getElementById (` ${ color} -${ value} ` )
101+ const colorValue = window .getComputedStyle (el).backgroundColor
102+ const [r , g , b ] = colorValue .match (/ ^ rgba? \( (\d + ),\s * (\d + ),\s * (\d + )(?:,\s * (\d + (?:\. \d + )? ))? \) $ / ).slice (1 )
103+
104+ // HSP (Highly Sensitive Poo) equation from http://alienryderflex.com/hsp.html
105+ const hsp = Math .sqrt (
106+ 0.299 * (r * r) +
107+ 0.587 * (g * g) +
108+ 0.114 * (b * b)
109+ )
110+
111+ // Using the HSP value, determine whether the color is light or dark
112+ if (hsp > 127.5 ) {
113+ return ' light'
114+ }
115+ return ' dark'
116+ },
90117 async copy (text ) {
91118 if (! navigator .clipboard ) { return }
92119 this ._timeout && clearTimeout (this ._timeout )
0 commit comments