UNPKG

4.95 kBJavaScriptView Raw
1// 'transparent' at beginning of array as it's not really a color,
2// but needs to be recognized as well
3export const CSS_COLOR_NAMES = [
4 'transparent', 'aliceblue', 'antiquewhite', 'aqua', 'aquamarine', 'azure',
5 'beige', 'bisque', 'black', 'blanchedalmond', 'blue', 'blueviolet', 'brown',
6 'burlywood', 'cadetblue', 'chartreuse', 'chocolate', 'coral', 'cornflowerblue',
7 'cornsilk', 'crimson', 'cyan', 'darkblue', 'darkcyan', 'darkgoldenrod',
8 'darkgray', 'darkgrey', 'darkgreen', 'darkkhaki', 'darkmagenta', 'darkolivegreen',
9 'darkorange', 'darkorchid', 'darkred', 'darksalmon', 'darkseagreen', 'darkslateblue',
10 'darkslategray', 'darkslategrey', 'darkturquoise', 'darkviolet', 'deeppink',
11 'deepskyblue', 'dimgray', 'dimgrey', 'dodgerblue', 'firebrick', 'floralwhite',
12 'forestgreen', 'fuchsia', 'gainsboro', 'ghostwhite', 'gold', 'goldenrod', 'gray',
13 'grey', 'green', 'greenyellow', 'honeydew', 'hotpink', 'indianred', 'indigo',
14 'ivory', 'khaki', 'lavender', 'lavenderblush', 'lawngreen', 'lemonchiffon',
15 'lightblue', 'lightcoral', 'lightcyan', 'lightgoldenrodyellow', 'lightgray',
16 'lightgrey', 'lightgreen', 'lightpink', 'lightsalmon', 'lightseagreen', 'lightskyblue',
17 'lightslategray', 'lightslategrey', 'lightsteelblue', 'lightyellow', 'lime', 'limegreen',
18 'linen', 'magenta', 'maroon', 'mediumaquamarine', 'mediumblue', 'mediumorchid',
19 'mediumpurple', 'mediumseagreen', 'mediumslateblue', 'mediumspringgreen',
20 'mediumturquoise', 'mediumvioletred', 'midnightblue', 'mintcream', 'mistyrose',
21 'moccasin', 'navajowhite', 'navy', 'oldlace', 'olive', 'olivedrab', 'orange',
22 'orangered', 'orchid', 'palegoldenrod', 'palegreen', 'paleturquoise', 'palevioletred',
23 'papayawhip', 'peachpuff', 'peru', 'pink', 'plum', 'powderblue', 'purple', 'red',
24 'rosybrown', 'royalblue', 'saddlebrown', 'salmon', 'sandybrown', 'seagreen', 'seashell',
25 'sienna', 'silver', 'skyblue', 'slateblue', 'slategray', 'slategrey', 'snow',
26 'springgreen', 'steelblue', 'tan', 'teal', 'thistle', 'tomato', 'turquoise', 'violet',
27 'wheat', 'white', 'whitesmoke', 'yellow', 'yellowgreen'
28]
29
30export default {
31 name: 'QColorizeMixin',
32
33 props: {
34 color: String
35 },
36
37 methods: {
38 isNamedCssColor (color) {
39 return !!color && CSS_COLOR_NAMES.includes(color.toLowerCase())
40 },
41
42 isCssColor (color) {
43 return !!color && (!!color.match(/^(#|(rgb|hsl)a?\()/) || this.isNamedCssColor(color))
44 },
45
46 isCssVar (color) {
47 return !!color && color.startsWith('--')
48 },
49
50 calculateColor (color, defaultColor = 'black') {
51 return color === void 0 && defaultColor !== void 0 // safety net
52 ? this.calculateColor(defaultColor)
53 : this.isCssColor(color)
54 ? color
55 : this.makeQuasarColorVar(color, defaultColor)
56 },
57
58 makeQuasarColorVar (color, defaultColor) {
59 const varStr = this.isCssVar(color)
60 ? color
61 : `--q-color-${color}`
62
63 // return as a css string, ex: "var(--my-color, 'defaultColor')"
64 return `var(${varStr}, '${defaultColor}')`
65 },
66
67 isValidCssColor (color) {
68 return this.isCssColor(color) || this.isCssVar(color)
69 },
70
71 setBothColors (color, bgColor, data = {}) {
72 return this.setTextColor(color, this.setBackgroundColor(bgColor, data))
73 },
74
75 setBackgroundColor (color, data = {}) {
76 if (this.isValidCssColor(color)) {
77 const calcColor = this.calculateColor(color)
78 if (data.style === void 0) data.style = {}
79 data.style = {
80 ...data.style,
81 'background-color': `${calcColor}`
82 }
83 } else if (color) {
84 const colorName = color.toString().trim()
85 if (data.class === void 0) data.class = {}
86 data.class = {
87 ...data.class,
88 ['bg-' + colorName]: true
89 }
90 }
91
92 return data
93 },
94
95 setTextColor (color, data = {}) {
96 if (this.isValidCssColor(color)) {
97 const calcColor = this.calculateColor(color)
98 if (data.style === void 0) data.style = {}
99 data.style = {
100 ...data.style,
101 'color': `${calcColor}`,
102 'caret-color': `${calcColor}`
103 }
104 } else if (color) {
105 const colorName = color.toString().trim()
106 if (data.class === void 0) data.class = {}
107 data.class = {
108 ...data.class,
109 ['text-' + colorName]: true
110 }
111 }
112 return data
113 },
114
115 setBorderColor (color, data = {}) {
116 if (this.isValidCssColor(color)) {
117 const calcColor = this.calculateColor(color)
118 if (data.style === void 0) data.style = {}
119 data.style = {
120 ...data.style,
121 'border-color': `${calcColor}`
122 }
123 } else if (color) {
124 const colorName = color.toString().trim()
125 if (data.class === void 0) data.class = {}
126 data.class = {
127 ...data.class,
128 ['border-' + colorName]: true
129 }
130 }
131 return data
132 }
133 }
134}