@@ -53,31 +53,73 @@ const nonTextScoreG = fontScoreGmin[4][2]; // level to break to non-text only
53
53
// 999 = too low. 777 = non-text and spot text only
54
54
55
55
const byFontSize = [
56
- [ 'px' , 100 , 200 , 300 , 400 , 500 , 600 , 700 , 800 , 900 ] ,
57
- [ 10 , 999 , 999 , 999 , 999 , 999 , 999 , 999 , 999 , 999 ] ,
58
- [ 12 , 999 , 999 , 999 , 777 , 777 , 777 , 777 , 999 , 999 ] ,
59
- [ 14 , 999 , 999 , 777 , 100 , 100 , 90 , 75 , 999 , 999 ] ,
60
- [ 15 , 999 , 999 , 777 , 100 , 90 , 75 , 70 , 999 , 999 ] ,
61
- [ 16 , 999 , 999 , 777 , 90 , 75 , 70 , 60 , 60 , 999 ] ,
62
- [ 18 , 999 , 777 , 100 , 75 , 70 , 60 , 55 , 55 , 55 ] ,
63
- [ 21 , 999 , 777 , 90 , 70 , 60 , 55 , 50 , 50 , 50 ] ,
64
- [ 24 , 999 , 777 , 75 , 60 , 55 , 50 , 45 , 45 , 45 ] ,
65
- [ 28 , 999 , 100 , 70 , 55 , 50 , 45 , 43 , 43 , 43 ] ,
66
- [ 32 , 999 , 90 , 65 , 50 , 45 , 43 , 40 , 40 , 40 ] ,
67
- [ 36 , 999 , 75 , 60 , 45 , 43 , 40 , 38 , 38 , 38 ] ,
68
- [ 42 , 100 , 70 , 55 , 43 , 40 , 38 , 35 , 35 , 35 ] ,
69
- [ 48 , 90 , 60 , 50 , 40 , 38 , 35 , 33 , 33 , 33 ] ,
70
- [ 60 , 75 , 55 , 45 , 38 , 35 , 33 , 30 , 30 , 30 ] ,
71
- [ 72 , 60 , 50 , 40 , 35 , 33 , 30 , 30 , 30 , 30 ] ,
72
- [ 96 , 50 , 45 , 35 , 33 , 30 , 30 , 30 , 30 , 30 ] ,
56
+ [ 'px' , 100 , 200 , 300 , 400 , 500 , 600 , 700 , 800 , 900 ] ,
57
+ [ 10 , { Lc : 999 } , { Lc : 999 } , { Lc : 999 } , { Lc : 999 } , { Lc : 999 } , { Lc : 999 } , { Lc : 999 } , { Lc : 999 } , { Lc : 999 } ] ,
58
+ [ 12 , { Lc : 999 } , { Lc : 999 } , { Lc : 999 } , { Lc : 777 } , { Lc : 777 } , { Lc : 777 } , { Lc : 777 } , { Lc : 999 } , { Lc : 999 } ] ,
59
+ [ 14 , { Lc : 999 } , { Lc : 999 } , { Lc : 777 } , { Lc : 100 } , { Lc : 100 } , { Lc : 90 } , { Lc : 75 } , { Lc : 999 } , { Lc : 999 } ] ,
60
+ [ 15 , { Lc : 999 } , { Lc : 999 } , { Lc : 777 } , { Lc : 100 } , { Lc : 90 } , { Lc : 75 } , { Lc : 70 } , { Lc : 999 } , { Lc : 999 } ] ,
61
+ [ 16 , { Lc : 999 } , { Lc : 999 } , { Lc : 777 } , { Lc : 90 } , { Lc : 75 } , { Lc : 70 } , { Lc : 60 } , { Lc : 60 } , { Lc : 999 } ] ,
62
+ [ 18 , { Lc : 999 } , { Lc : 777 } , { Lc : 100 } , { Lc : 75 } , { Lc : 70 } , { Lc : 60 } , { Lc : 55 } , { Lc : 55 } , { Lc : 55 } ] ,
63
+ [ 21 , { Lc : 999 } , { Lc : 777 } , { Lc : 90 } , { Lc : 70 } , { Lc : 60 } , { Lc : 55 } , { Lc : 50 } , { Lc : 50 } , { Lc : 50 } ] ,
64
+ [ 24 , { Lc : 999 } , { Lc : 777 } , { Lc : 75 } , { Lc : 60 } , { Lc : 55 } , { Lc : 50 } , { Lc : 45 } , { Lc : 45 } , { Lc : 45 } ] ,
65
+ [ 28 , { Lc : 999 } , { Lc : 100 } , { Lc : 70 } , { Lc : 55 } , { Lc : 50 } , { Lc : 45 } , { Lc : 43 } , { Lc : 43 } , { Lc : 43 } ] ,
66
+ [ 32 , { Lc : 999 } , { Lc : 90 } , { Lc : 65 } , { Lc : 50 } , { Lc : 45 } , { Lc : 43 } , { Lc : 40 } , { Lc : 40 } , { Lc : 40 } ] ,
67
+ [ 36 , { Lc : 999 } , { Lc : 75 } , { Lc : 60 } , { Lc : 45 } , { Lc : 43 } , { Lc : 40 } , { Lc : 38 } , { Lc : 38 } , { Lc : 38 } ] ,
68
+ [ 42 , { Lc : 100 } , { Lc : 70 } , { Lc : 55 } , { Lc : 43 } , { Lc : 40 } , { Lc : 38 } , { Lc : 35 } , { Lc : 35 } , { Lc : 35 } ] ,
69
+ [ 48 , { Lc : 90 } , { Lc : 60 } , { Lc : 50 } , { Lc : 40 } , { Lc : 38 } , { Lc : 35 } , { Lc : 33 } , { Lc : 33 } , { Lc : 33 } ] ,
70
+ [ 60 , { Lc : 75 } , { Lc : 55 } , { Lc : 45 } , { Lc : 38 } , { Lc : 35 } , { Lc : 33 } , { Lc : 30 } , { Lc : 30 } , { Lc : 30 } ] ,
71
+ [ 72 , { Lc : 60 } , { Lc : 50 } , { Lc : 40 } , { Lc : 35 } , { Lc : 33 } , { Lc : 30 } , { Lc : 30 } , { Lc : 30 } , { Lc : 30 } ] ,
72
+ [ 96 , { Lc : 50 } , { Lc : 45 } , { Lc : 35 } , { Lc : 33 } , { Lc : 30 } , { Lc : 30 } , { Lc : 30 } , { Lc : 30 } , { Lc : 30 } ] ,
73
73
] ;
74
74
75
75
export function gettxt ( color , knownType , fs , fw , usage ) {
76
+ if ( fw <= 100 ) console . warn ( "Avoid using font-weight 100" ) ;
77
+
76
78
const [ fontWeights , ...rest ] = byFontSize ;
77
79
const closestFontSize = rest . reduce ( ( x , y ) => Math . abs ( y [ 0 ] - fs ) < Math . abs ( x [ 0 ] - fs ) ? y : x ) ;
78
- const Lc = closestFontSize [ fontWeights . indexOf ( fw ) ] ;
79
- const target = fontScoreGmin . reduce ( ( x , y ) => Math . abs ( y [ 0 ] - Lc ) < Math . abs ( x [ 0 ] - Lc ) ? y : x ) [ usage ] ;
80
+ let Lc = closestFontSize [ fontWeights . indexOf ( + fw ) ] . Lc ;
81
+
82
+ switch ( usage . toUpperCase ( ) ) {
83
+ case "DECORATIVE" : usage = 1 ; break ;
84
+ case "SPOT" : usage = 2 ; break ;
85
+ case "SUBFLUENT" : usage = 3 ; break ;
86
+ case "FLUENT" : usage = 4 ; break ;
87
+ case "BODYTEXT" : usage = 5 ; break ;
88
+ case "MAX" : usage = 6 ; break ;
89
+ }
80
90
91
+ if ( Lc === 999 ) {
92
+ if ( usage !== 1 ) {
93
+ return console . log (
94
+ `Got font-size = ${ fs } px and font-weight = ${ fw } .` ,
95
+ "Too low. Prohibited Except for Decorative Purposes. Maybe you forgot to make the usage = Decorative."
96
+ ) ;
97
+ }
98
+ Lc = minScoreG ;
99
+ } else if ( Lc === 777 ) {
100
+ if ( usage !== 2 ) {
101
+ return console . log (
102
+ `Got font-size = ${ fs } px and font-weight = ${ fw } .` ,
103
+ "Just for non-text and spot text only. Maybe you forgot to make the usage = Spot. If not try to increase the font size or font weight."
104
+ ) ;
105
+ }
106
+ Lc = nonTextScoreG ;
107
+ } else if ( Lc < nonTextScoreG && usage > 3 ) {
108
+ return console . log (
109
+ `Got font-size = ${ fs } px and font-weight = ${ fw } .` ,
110
+ "Contrast too low for text. Try decreasing the font size or the font weight."
111
+ ) ;
112
+ } else if (
113
+ fw >= 300 && fw <= 700 &&
114
+ fs >= 14 && fs <= 36 &&
115
+ Lc > nonTextScoreG &&
116
+ usage > 4
117
+ ) {
118
+ Lc += 15 ;
119
+ }
120
+
121
+ const target = fontScoreGmin . reduce ( ( x , y ) => Math . abs ( y [ 0 ] - Lc ) < Math . abs ( x [ 0 ] - Lc ) ? y : x ) [ usage ] ;
122
+
81
123
const { h, s } = toHSL ( color ) ;
82
124
83
125
const colors = [ ] ;
@@ -86,8 +128,8 @@ export function gettxt(color, knownType, fs, fw, usage) {
86
128
87
129
let contrast ;
88
130
switch ( knownType ) {
89
- case "txt " : contrast = calcAPCA ( newColor , color ) ; break ;
90
- case "bg " : contrast = calcAPCA ( color , newColor ) ; break ;
131
+ case "bg " : contrast = calcAPCA ( newColor , color ) ; break ;
132
+ case "txt " : contrast = calcAPCA ( color , newColor ) ; break ;
91
133
}
92
134
93
135
colors . push ( {
@@ -98,6 +140,7 @@ export function gettxt(color, knownType, fs, fw, usage) {
98
140
99
141
const { color : contrast } = colors . reduce ( ( x , y ) => Math . abs ( Math . abs ( y . contrast ) - target ) < Math . abs ( Math . abs ( x . contrast ) - target ) ? y : x ) ;
100
142
143
+ /*
101
144
const colors3 = [];
102
145
for (let i = 0; i <= 100; i++) {
103
146
const newColor = toHSLString({h: 0, s: 0, l: i});
@@ -123,6 +166,7 @@ export function gettxt(color, knownType, fs, fw, usage) {
123
166
contrast2 = toHSLString(toHSL(contrast2));
124
167
125
168
//console.log(target, contrast, contrast2, contrast3);
169
+ */
126
170
127
171
return contrast ;
128
172
}
@@ -153,7 +197,11 @@ function toHSL(color) {
153
197
}
154
198
} ) ( ) * 60 ;
155
199
156
- return { h, s : s * 100 + "%" , l : l * 100 + "%" } ;
200
+ return {
201
+ h : Math . round ( h ) ,
202
+ s : Math . round ( s * 100 ) + "%" ,
203
+ l : Math . round ( l * 100 ) + "%" ,
204
+ } ;
157
205
}
158
206
159
207
function toHSLString ( { h, s, l} ) {
0 commit comments