File size: 3,198 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { hexToRgb } from '@automattic/onboarding';
import debugFactory from 'debug';
import type {
	StyleVariation,
	StyleVariationSettingsColorPalette,
	StyleVariationStylesColor,
} from '../../types';

interface Hsl {
	h: number;
	s: number;
	l: number;
}

const debug = debugFactory( 'design-picker:style-variation-badges' );

const COLOR_BASE_CANDIDATE_KEYS = [ 'base', 'background', 'primary' ];
const HSL_BEST_DIFFERENCE_VALUE = 155;

function getColors( variation: StyleVariation ) {
	return variation.settings?.color?.palette?.theme || [];
}

function getColorBaseFromColors( colors: StyleVariationSettingsColorPalette[] ) {
	const colorMap: Record< string, string > = colors.reduce(
		( map, { color, slug } ) => ( { ...map, [ slug ]: color } ),
		{}
	);

	const baseColorKey = COLOR_BASE_CANDIDATE_KEYS.find( ( key ) => colorMap[ key ] ) ?? '';
	return colorMap[ baseColorKey ] || '#ffffff';
}

// See: https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB
function hexToHsl( hexCode: string ) {
	const { r, g, b } = hexToRgb( hexCode );
	const max = Math.max( r, g, b );
	const min = Math.min( r, g, b );
	const l = ( max + min ) / 2;
	let h = 0;
	let s = 0;

	if ( max !== min ) {
		const c = max - min;
		s = l > 0.5 ? c / ( 2 - max - min ) : c / ( max + min );

		switch ( max ) {
			case r:
				h = ( g - b ) / c + ( g < b ? 6 : 0 );
				break;

			case g:
				h = ( b - r ) / c + 2;
				break;

			case b:
				h = ( r - g ) / c + 4;
				break;

				h /= 6;
		}
	}

	return { h, s, l };
}

function getColorAnalogous( { h, s, l }: Hsl ) {
	const hA = ( h + 1 / 12 ) % 1;
	const hB = ( h - 1 / 12 + 1 ) % 1;
	return [
		{ h: hA, s, l },
		{ h: hB, s, l },
	];
}

function getHslDifference( hslA: Hsl, hslB: Hsl ) {
	const { h: hA, s: sA, l: lA } = hslA;
	const { h: hB, s: sB, l: lB } = hslB;
	return Math.abs( hA - hB ) + Math.abs( sA - sB ) + Math.abs( lA - lB );
}

function getHslBestDifference( value: number ) {
	return Math.abs( value - HSL_BEST_DIFFERENCE_VALUE );
}

function findColorBestAnalogous( hexCodes: string[], baseHex: string ) {
	const baseHsl = hexToHsl( baseHex );
	const analogous = getColorAnalogous( baseHsl );

	let bestHslDifference = -Infinity;
	let bestAnalogous = '';
	for ( const hex of hexCodes ) {
		let hsl;

		// We can't convert the hex to hsl if the value is something like `color-mix(in srgb, currentColor 20%, transparent)`
		try {
			hsl = hexToHsl( hex );
		} catch ( e ) {
			debug( e );
			continue;
		}

		const hslDifference = Math.max(
			getHslDifference( analogous[ 0 ], hsl ),
			getHslDifference( analogous[ 1 ], hsl )
		);

		if ( getHslBestDifference( hslDifference ) < getHslBestDifference( bestHslDifference ) ) {
			bestHslDifference = hslDifference;
			bestAnalogous = hex;
		}
	}

	return bestAnalogous;
}

export function getStylesColorFromVariation(
	variation: StyleVariation
): StyleVariationStylesColor | null {
	const palette = getColors( variation );
	const colorBase = getColorBaseFromColors( palette );
	const colorList = palette.map( ( item ) => item.color );

	try {
		return { background: colorBase, text: findColorBestAnalogous( colorList, colorBase ) };
	} catch ( e ) {
		debug( e, variation );
		return null;
	}
}