|
1 | 1 | import * as P from 'polished'
|
2 | 2 | import { get, Theme } from '@theme-ui/css'
|
3 | 3 |
|
4 |
| -const g = (t: Theme, c: string) => |
5 |
| - get(t, `colors.${c}`, c) |
| 4 | +/** |
| 5 | + * Get color from theme.colors |
| 6 | + */ |
| 7 | +export const getColor = (theme: Theme, color: string) => |
| 8 | + get(theme, `colors.${color}`, color) |
6 | 9 | .replace(/^var\(--(\w+)(.*?), /, '')
|
7 | 10 | .replace(/\)/, '')
|
8 | 11 |
|
9 | 12 | /**
|
10 | 13 | * Darken a color by an amount 0–1
|
11 | 14 | */
|
12 | 15 | export const darken = (c: string, n: number) => (t: Theme) =>
|
13 |
| - P.darken(n, g(t, c)) |
| 16 | + P.darken(n, getColor(t, c)) |
14 | 17 | /**
|
15 | 18 | * Lighten a color by an amount 0–1
|
16 | 19 | */
|
17 | 20 | export const lighten = (c: string, n: number) => (t: Theme) =>
|
18 |
| - P.lighten(n, g(t, c)) |
| 21 | + P.lighten(n, getColor(t, c)) |
19 | 22 | /**
|
20 | 23 | * Rotate the hue of a color by an amount 0–360
|
21 | 24 | */
|
22 | 25 | export const rotate = (c: string, d: number) => (t: Theme) =>
|
23 |
| - P.adjustHue(d, g(t, c)) |
| 26 | + P.adjustHue(d, getColor(t, c)) |
24 | 27 |
|
25 | 28 | /**
|
26 | 29 | * Set the hue of a color to a degree 0–360
|
27 | 30 | */
|
28 |
| -export const hue = (c: string, h: number) => (t: Theme) => P.setHue(h, g(t, c)) |
| 31 | +export const hue = (c: string, h: number) => (t: Theme) => |
| 32 | + P.setHue(h, getColor(t, c)) |
29 | 33 | /**
|
30 | 34 | * Set the saturation of a color to an amount 0–1
|
31 | 35 | */
|
32 | 36 | export const saturation = (c: string, s: number) => (t: Theme) =>
|
33 |
| - P.setSaturation(s, g(t, c)) |
| 37 | + P.setSaturation(s, getColor(t, c)) |
34 | 38 | /**
|
35 | 39 | * Set the lightness of a color to an amount 0–1
|
36 | 40 | */
|
37 | 41 | export const lightness = (c: string, l: number) => (t: Theme) =>
|
38 |
| - P.setLightness(l, g(t, c)) |
| 42 | + P.setLightness(l, getColor(t, c)) |
39 | 43 | /**
|
40 | 44 | * Desaturate a color by an amount 0–1
|
41 | 45 | */
|
42 | 46 | export const desaturate = (c: string, n: number) => (t: Theme) =>
|
43 |
| - P.desaturate(n, g(t, c)) |
| 47 | + P.desaturate(n, getColor(t, c)) |
44 | 48 | /**
|
45 | 49 | * Saturate a color by an amount 0–1
|
46 | 50 | */
|
47 | 51 | export const saturate = (c: string, n: number) => (t: Theme) =>
|
48 |
| - P.saturate(n, g(t, c)) |
| 52 | + P.saturate(n, getColor(t, c)) |
49 | 53 |
|
50 | 54 | /**
|
51 | 55 | * Shade a color by an amount 0–1
|
52 | 56 | */
|
53 |
| -export const shade = (c: string, n: number) => (t: Theme) => P.shade(n, g(t, c)) |
| 57 | +export const shade = (c: string, n: number) => (t: Theme) => |
| 58 | + P.shade(n, getColor(t, c)) |
54 | 59 | /**
|
55 | 60 | * Tint a color by an amount 0–1
|
56 | 61 | */
|
57 |
| -export const tint = (c: string, n: number) => (t: Theme) => P.tint(n, g(t, c)) |
| 62 | +export const tint = (c: string, n: number) => (t: Theme) => |
| 63 | + P.tint(n, getColor(t, c)) |
58 | 64 |
|
59 | 65 | export const transparentize = (c: string, n: number) => (t: Theme) =>
|
60 |
| - P.transparentize(n, g(t, c)) |
| 66 | + P.transparentize(n, getColor(t, c)) |
61 | 67 | /**
|
62 | 68 | * Set the transparency of a color to an amount 0-1
|
63 | 69 | */
|
64 |
| -export const alpha = (c: string, n: number) => (t: Theme) => P.rgba(g(t, c), n) |
| 70 | +export const alpha = (c: string, n: number) => (t: Theme) => |
| 71 | + P.rgba(getColor(t, c), n) |
65 | 72 |
|
66 | 73 | /**
|
67 | 74 | * Mix two colors by a specific ratio
|
68 | 75 | */
|
69 | 76 | export const mix = (a: string, b: string, n = 0.5) => (t: Theme) =>
|
70 |
| - P.mix(n, g(t, a), g(t, b)) |
| 77 | + P.mix(n, getColor(t, a), getColor(t, b)) |
71 | 78 |
|
72 | 79 | /**
|
73 | 80 | * Get the complement of a color
|
74 | 81 | */
|
75 |
| -export const complement = (c: string) => (t: Theme) => P.complement(g(t, c)) |
| 82 | +export const complement = (c: string) => (t: Theme) => |
| 83 | + P.complement(getColor(t, c)) |
76 | 84 |
|
77 | 85 | /**
|
78 | 86 | * Get the inverted color
|
79 | 87 | */
|
80 |
| -export const invert = (c: string) => (t: Theme) => P.invert(g(t, c)) |
| 88 | +export const invert = (c: string) => (t: Theme) => P.invert(getColor(t, c)) |
81 | 89 |
|
82 | 90 | /**
|
83 | 91 | * Desaturate the color to grayscale
|
|
0 commit comments