@@ -9,10 +9,11 @@ OF ANY KIND, either express or implied. See the License for the specific languag
9
9
governing permissions and limitations under the License.
10
10
*/
11
11
12
- import d3 from './d3' ;
12
+ import { APCAcontrast , sRGBtoY } from "apca-w3" ;
13
13
import {
14
14
round
15
15
} from './utils' ;
16
+ import { contrast } from '@adobe/leonardo-contrast-colors' ;
16
17
import { saveAs } from 'file-saver' ;
17
18
18
19
const simpleColorConverter = require ( 'simple-color-converter' ) ;
@@ -38,6 +39,7 @@ function cancelBulkConvert() {
38
39
}
39
40
40
41
function bulkItemConvertColorInput ( e ) {
42
+ let background = document . getElementById ( 'bulkConvertBackground' ) . value ;
41
43
let bulkInputs = document . getElementById ( 'bulkConvertColors' ) ;
42
44
let bulkValues = bulkInputs . value . replace ( / \r \n / g, "\n" ) . replace ( / [ , \/ ] / g, "\n" ) . replace ( " " , "" ) . replace ( / [ ' \/ ] / g, "" ) . replace ( / [ " \/ ] / g, "" ) . replace ( " " , "" ) . split ( "\n" ) ;
43
45
bulkValues = bulkValues . map ( ( value ) => { return value . replace ( " " , "" ) } ) ;
@@ -64,11 +66,9 @@ function bulkItemConvertColorInput(e) {
64
66
65
67
let data = [ ] ;
66
68
colors . map ( ( c ) => {
67
- data . push ( createColorJson ( c , opts ) )
69
+ data . push ( createColorJson ( c , opts , background ) )
68
70
} )
69
71
70
- // console.log(data)
71
-
72
72
const replacer = ( key , value ) => value === null ? '' : value // specify how you want to handle null values here
73
73
const header = Object . keys ( data [ 0 ] )
74
74
const csv = [
@@ -93,7 +93,7 @@ function bulkItemConvertColorInput(e) {
93
93
bulkInputs . value = " " ;
94
94
}
95
95
96
- function createColorJson ( value , colorSpaces ) {
96
+ function createColorJson ( value , colorSpaces , background ) {
97
97
if ( ! chroma . valid ( value ) ) {
98
98
// Should return an error
99
99
}
@@ -119,6 +119,13 @@ function createColorJson(value, colorSpaces) {
119
119
if ( colorSpaces [ i ] === 'CMYK' ) colorChannels = color . cmyk ( ) . map ( ( c ) => { return c * 100 } ) ;
120
120
if ( colorSpaces [ i ] === 'XYZ' ) colorChannels = Object . values ( new simpleColorConverter ( { rgb : { r : color . rgb ( ) [ 0 ] , g : color . rgb ( ) [ 1 ] , b : color . rgb ( ) [ 2 ] } , to : 'xyz' } ) . color ) ;
121
121
if ( colorSpaces [ i ] === 'Pantone' ) colorChannels = new simpleColorConverter ( { rgb : { r : color . rgb ( ) [ 0 ] , g : color . rgb ( ) [ 1 ] , b : color . rgb ( ) [ 2 ] } , to : 'pantone' } ) . color ;
122
+ if ( colorSpaces [ i ] === 'wcag2' ) colorChannels = round ( contrast ( color . rgb ( ) , chroma ( background ) . rgb ( ) , 'wcag2' ) , 2 ) ;
123
+ if ( colorSpaces [ i ] === 'wcag3' ) {
124
+ const baseLightness = chroma ( background ) . hsluv ( ) [ 2 ] ;
125
+ const baseV = round ( baseLightness / 100 , 2 ) ;
126
+ let base = chroma ( background ) . rgb ( ) ;
127
+ colorChannels = round ( ( baseV < 0.5 ) ? APCAcontrast ( sRGBtoY ( color . rgb ( ) ) , sRGBtoY ( base ) ) * - 1 : APCAcontrast ( sRGBtoY ( color . rgb ( ) ) , sRGBtoY ( base ) ) , 2 ) ;
128
+ }
122
129
123
130
if ( colorSpaces [ i ] === 'HEX' ) colorChannelLabels = [ 'Hex' ] ;
124
131
if ( colorSpaces [ i ] === 'RGB' ) colorChannelLabels = [ 'rgb (R)' , 'rgb (G)' , 'rgb (B)' ] ;
@@ -135,6 +142,10 @@ function createColorJson(value, colorSpaces) {
135
142
if ( colorSpaces [ i ] === 'XYZ' ) colorChannelLabels = [ 'xyz (X)' , 'xyz (Y)' , 'xyz (Z)' ] ;
136
143
if ( colorSpaces [ i ] === 'Pantone' ) colorChannelLabels = [ 'Pantone' ] ;
137
144
145
+ if ( colorSpaces [ i ] === 'wcag2' ) colorChannelLabels = [ `Rel Lum vs ${ background } ` ]
146
+ if ( colorSpaces [ i ] === 'wcag3' ) colorChannelLabels = [ `APCA vs ${ background } ` ]
147
+
148
+
138
149
if ( Array . isArray ( colorChannels ) ) {
139
150
colorChannels = colorChannels . map ( ( c ) => { return round ( c , 2 ) } )
140
151
} else {
0 commit comments