8
8
9
9
<script lang="ts">
10
10
import { computed , defineComponent } from " @nuxtjs/composition-api" ;
11
+ // @ts-ignore missing color types
12
+ import Color from " @sphinxxxx/color-conversion" ;
11
13
import { MultiPurposeLabelSummary } from " ~/lib/api/types/recipe" ;
12
14
15
+
13
16
export default defineComponent ({
14
17
props: {
15
18
label: {
@@ -34,19 +37,27 @@ export default defineComponent({
34
37
const ACCESSIBILITY_THRESHOLD = 0.179 ;
35
38
36
39
function pickTextColorBasedOnBgColorAdvanced(bgColor : string , lightColor : string , darkColor : string ) {
37
- const color = bgColor .charAt (0 ) === " #" ? bgColor .substring (1 , 7 ) : bgColor ;
38
- const r = parseInt (color .substring (0 , 2 ), 16 ); // hexToR
39
- const g = parseInt (color .substring (2 , 4 ), 16 ); // hexToG
40
- const b = parseInt (color .substring (4 , 6 ), 16 ); // hexToB
41
- const uicolors = [r / 255 , g / 255 , b / 255 ];
42
- const c = uicolors .map ((col ) => {
43
- if (col <= 0.03928 ) {
44
- return col / 12.92 ;
40
+ try {
41
+ const color = new Color (bgColor );
42
+
43
+ // if opacity is less than 0.3 always return dark color
44
+ if (color ._rgba [3 ] < 0.3 ) {
45
+ return darkColor ;
45
46
}
46
- return Math .pow ((col + 0.055 ) / 1.055 , 2.4 );
47
- });
48
- const L = 0.2126 * c [0 ] + 0.7152 * c [1 ] + 0.0722 * c [2 ];
49
- return L > ACCESSIBILITY_THRESHOLD ? darkColor : lightColor ;
47
+
48
+ const uicolors = [color ._rgba [0 ] / 255 , color ._rgba [1 ] / 255 , color ._rgba [2 ] / 255 ];
49
+ const c = uicolors .map ((col ) => {
50
+ if (col <= 0.03928 ) {
51
+ return col / 12.92 ;
52
+ }
53
+ return Math .pow ((col + 0.055 ) / 1.055 , 2.4 );
54
+ });
55
+ const L = 0.2126 * c [0 ] + 0.7152 * c [1 ] + 0.0722 * c [2 ];
56
+ return L > ACCESSIBILITY_THRESHOLD ? darkColor : lightColor ;
57
+ } catch (error ) {
58
+ console .warn (error );
59
+ return " black" ;
60
+ }
50
61
}
51
62
52
63
return {
0 commit comments