(hex: string, halfShades = false)
| 107 | * @returns {{[key: number]: string}} |
| 108 | */ |
| 109 | export function shadesOfColor(hex: string, halfShades = false) { |
| 110 | const baseColor = hex; |
| 111 | |
| 112 | const shades = [ |
| 113 | 50, |
| 114 | 100, |
| 115 | 200, |
| 116 | 300, |
| 117 | 400, |
| 118 | 500, |
| 119 | 600, |
| 120 | 700, |
| 121 | 800, |
| 122 | 900, |
| 123 | ...(halfShades ? [150, 250, 350, 450, 550, 650, 750, 850] : []), |
| 124 | ].sort(); |
| 125 | |
| 126 | const result: ColorShades = {}; |
| 127 | |
| 128 | for (const shade of shades) { |
| 129 | const key = shade.toString(); |
| 130 | |
| 131 | if (shade === 500) { |
| 132 | result[key] = hex; |
| 133 | continue; |
| 134 | } |
| 135 | |
| 136 | let shadeIndex = shade; |
| 137 | const isDarkShade = shadeIndex > 500; |
| 138 | if (isDarkShade) { |
| 139 | shadeIndex -= 500; |
| 140 | } |
| 141 | |
| 142 | const percentage = shadeIndex / 500; |
| 143 | const startColor = isDarkShade ? DARK_BASE : baseColor; |
| 144 | const endColor = isDarkShade ? baseColor : LIGHT_BASE; |
| 145 | |
| 146 | result[key] = getColor(percentage, hexToRgbArray(startColor), hexToRgbArray(endColor)); |
| 147 | } |
| 148 | |
| 149 | return result; |
| 150 | } |
| 151 | |
| 152 | export type ColorScaleOptions = { |
| 153 | /** If set to `true`, inverts the scale (so 1 is black instead of white) and uses `colorMixMapping.dark` with different mix ratios per step. */ |
no test coverage detected