-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add color utility functions and Tailwind CSS configuration
- Loading branch information
1 parent
5b41589
commit 5c9a32f
Showing
11 changed files
with
619 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import type { Mode, PaletteConfig } from './types'; | ||
|
||
export const DEFAULT_STOP = 500; | ||
export const DEFAULT_STOPS = [ | ||
0, 50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950, 1000, | ||
]; | ||
|
||
export const MODES: Mode[] = [`hex`, `p-3`, 'oklch']; | ||
export const DEFAULT_MODE = MODES[0]; | ||
|
||
export const DEFAULT_PALETTE_CONFIG: PaletteConfig = { | ||
id: ``, | ||
name: ``, | ||
value: ``, | ||
valueStop: DEFAULT_STOP, | ||
swatches: [], | ||
h: 0, | ||
s: 0, | ||
lMin: 0, | ||
lMax: 100, | ||
useLightness: true, | ||
mode: MODES[0], | ||
}; | ||
|
||
export const RANDOM_PALETTES = [ | ||
{ | ||
name: `blue`, | ||
value: `3B82F6`, | ||
}, | ||
{ | ||
name: `red`, | ||
value: `EF4444`, | ||
}, | ||
{ | ||
name: `green`, | ||
value: `22C55E`, | ||
}, | ||
{ | ||
name: `purple`, | ||
value: `A855F7`, | ||
}, | ||
{ | ||
name: `brand`, | ||
value: `2522FC`, | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { DEFAULT_PALETTE_CONFIG } from './constants'; | ||
import { | ||
clamp, | ||
hexToHSL, | ||
HSLToHex, | ||
lightnessFromHSLum, | ||
luminanceFromHex, | ||
unsignedModulo, | ||
} from './helpers'; | ||
import { | ||
createDistributionValues, | ||
createHueScale, | ||
createSaturationScale, | ||
} from './scales'; | ||
import type { PaletteConfig } from './types'; | ||
|
||
export function createSwatches(palette: PaletteConfig) { | ||
const { value, valueStop } = palette; | ||
|
||
// Tweaks may be passed in, otherwise use defaults | ||
const useLightness = | ||
palette.useLightness ?? DEFAULT_PALETTE_CONFIG.useLightness; | ||
const h = palette.h ?? DEFAULT_PALETTE_CONFIG.h; | ||
const s = palette.s ?? DEFAULT_PALETTE_CONFIG.s; | ||
const lMin = palette.lMin ?? DEFAULT_PALETTE_CONFIG.lMin; | ||
const lMax = palette.lMax ?? DEFAULT_PALETTE_CONFIG.lMax; | ||
|
||
// Create hue and saturation scales based on tweaks | ||
const hueScale = createHueScale(h, valueStop); | ||
const saturationScale = createSaturationScale(s, valueStop); | ||
|
||
// Get the base hex's H/S/L values | ||
const { h: valueH, s: valueS, l: valueL } = hexToHSL(value); | ||
|
||
// Create lightness scales based on tweak + lightness/luminance of current value | ||
const lightnessValue = useLightness ? valueL : luminanceFromHex(value); | ||
const distributionScale = createDistributionValues( | ||
lMin, | ||
lMax, | ||
lightnessValue, | ||
valueStop | ||
); | ||
|
||
const swatches = hueScale.map(({ stop }, stopIndex) => { | ||
const newH = unsignedModulo(valueH + hueScale[stopIndex].tweak, 360); | ||
const newS = clamp(valueS + saturationScale[stopIndex].tweak, 0, 100); | ||
let newL = useLightness | ||
? distributionScale[stopIndex].tweak | ||
: lightnessFromHSLum( | ||
newH, | ||
newS, | ||
distributionScale[stopIndex].tweak | ||
); | ||
newL = clamp(newL, 0, 100); | ||
|
||
const newHex = HSLToHex(newH, newS, newL); | ||
|
||
return { | ||
stop, | ||
// Sometimes the initial value is changed slightly during conversion, | ||
// overriding that with the original value | ||
hex: | ||
stop === valueStop | ||
? `#${value.toUpperCase()}` | ||
: newHex.toUpperCase(), | ||
// Used in graphs | ||
h: newH, | ||
hScale: | ||
((unsignedModulo(hueScale[stopIndex].tweak + 180, 360) - 180) / | ||
180) * | ||
50, | ||
s: newS, | ||
sScale: newS - 50, | ||
l: newL, | ||
}; | ||
}); | ||
|
||
return swatches; | ||
} |
Oops, something went wrong.