Commit 98bf1dc 1 parent 2eec7a6 commit 98bf1dc Copy full SHA for 98bf1dc
File tree 2 files changed +23
-11
lines changed
packages/components/src/color-palette
2 files changed +23
-11
lines changed Original file line number Diff line number Diff line change @@ -43,6 +43,7 @@ import type { WordPressComponentProps } from '../ui/context';
43
43
import type { DropdownProps } from '../dropdown/types' ;
44
44
import {
45
45
extractColorNameFromCurrentValue ,
46
+ getCompositeBackgroundColor ,
46
47
isMultiplePaletteArray ,
47
48
normalizeColorValue ,
48
49
showTransparentBackground ,
@@ -227,14 +228,10 @@ function UnforwardedColorPalette(
227
228
) ;
228
229
229
230
const colordColor = colord ( normalizedColorValue ?? '' ) ;
230
- const mixedColor =
231
- colordColor . alpha ( ) < 1
232
- ? colord ( '#fff' ) . mix (
233
- colordColor . alpha ( 1 ) ,
234
- colordColor . alpha ( )
235
- )
236
- : colordColor ;
237
-
231
+ const compositeColor = getCompositeBackgroundColor (
232
+ colord ( '#fff' ) ,
233
+ colordColor
234
+ ) ;
238
235
const valueWithoutLeadingHash = value ?. startsWith ( '#' )
239
236
? value . substring ( 1 )
240
237
: value ?? '' ;
@@ -285,8 +282,10 @@ function UnforwardedColorPalette(
285
282
: {
286
283
background : value ,
287
284
color :
288
- mixedColor . contrast ( ) >
289
- mixedColor . contrast ( '#000' )
285
+ compositeColor . contrast ( ) >
286
+ compositeColor . contrast (
287
+ '#000'
288
+ )
290
289
? '#fff'
291
290
: '#000' ,
292
291
}
Original file line number Diff line number Diff line change 2
2
* External dependencies
3
3
*/
4
4
import type { RefObject } from 'react' ;
5
+ import type { Colord } from 'colord' ;
5
6
import { colord , extend } from 'colord' ;
6
7
import namesPlugin from 'colord/plugins/names' ;
7
8
import a11yPlugin from 'colord/plugins/a11y' ;
9
+ import mixPlugin from 'colord/plugins/mix' ;
8
10
9
11
/**
10
12
* WordPress dependencies
@@ -16,7 +18,7 @@ import { __ } from '@wordpress/i18n';
16
18
*/
17
19
import type { ColorObject , ColorPaletteProps , PaletteObject } from './types' ;
18
20
19
- extend ( [ namesPlugin , a11yPlugin ] ) ;
21
+ extend ( [ namesPlugin , a11yPlugin , mixPlugin ] ) ;
20
22
21
23
export const extractColorNameFromCurrentValue = (
22
24
currentValue ?: ColorPaletteProps [ 'value' ] ,
@@ -96,3 +98,14 @@ export const normalizeColorValue = (
96
98
? colord ( computedBackgroundColor ) . toHex ( )
97
99
: value ;
98
100
} ;
101
+
102
+ // Composite with background color behind when the front background color is transparent
103
+ // to get the actual background color.
104
+ export const getCompositeBackgroundColor = (
105
+ behindColor : Colord ,
106
+ frontColor : Colord
107
+ ) => {
108
+ return frontColor . alpha ( ) < 1
109
+ ? behindColor . mix ( frontColor . alpha ( 1 ) , frontColor . alpha ( ) )
110
+ : frontColor ;
111
+ } ;
You can’t perform that action at this time.
0 commit comments