Skip to content

Commit 0a33fb6

Browse files
authored
Merge pull request #894 from hasparus/export-get-color
Export getColor from theme-ui/colors
2 parents 8cff147 + e05f48e commit 0a33fb6

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

packages/color/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ export default props => (
2626

2727
## API
2828

29+
### `getColor`
30+
31+
```js
32+
import { getColor } from '@theme-ui/color'
33+
// getColor(theme, 'primary')
34+
```
35+
2936
### `darken`
3037

3138
Darken a color by an amount 0–1
@@ -185,7 +192,7 @@ We can take the result of any of the above helper functions (which return a func
185192
${alpha('primary', 0.5)(t)}
186193
${alpha('secondary', 0.5)(t)}
187194
)
188-
`
195+
`,
189196
}}
190197
/>
191198
```

packages/color/src/index.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,91 @@
11
import * as P from 'polished'
22
import { get, Theme } from '@theme-ui/css'
33

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)
69
.replace(/^var\(--(\w+)(.*?), /, '')
710
.replace(/\)/, '')
811

912
/**
1013
* Darken a color by an amount 0–1
1114
*/
1215
export const darken = (c: string, n: number) => (t: Theme) =>
13-
P.darken(n, g(t, c))
16+
P.darken(n, getColor(t, c))
1417
/**
1518
* Lighten a color by an amount 0–1
1619
*/
1720
export const lighten = (c: string, n: number) => (t: Theme) =>
18-
P.lighten(n, g(t, c))
21+
P.lighten(n, getColor(t, c))
1922
/**
2023
* Rotate the hue of a color by an amount 0–360
2124
*/
2225
export const rotate = (c: string, d: number) => (t: Theme) =>
23-
P.adjustHue(d, g(t, c))
26+
P.adjustHue(d, getColor(t, c))
2427

2528
/**
2629
* Set the hue of a color to a degree 0–360
2730
*/
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))
2933
/**
3034
* Set the saturation of a color to an amount 0–1
3135
*/
3236
export const saturation = (c: string, s: number) => (t: Theme) =>
33-
P.setSaturation(s, g(t, c))
37+
P.setSaturation(s, getColor(t, c))
3438
/**
3539
* Set the lightness of a color to an amount 0–1
3640
*/
3741
export const lightness = (c: string, l: number) => (t: Theme) =>
38-
P.setLightness(l, g(t, c))
42+
P.setLightness(l, getColor(t, c))
3943
/**
4044
* Desaturate a color by an amount 0–1
4145
*/
4246
export const desaturate = (c: string, n: number) => (t: Theme) =>
43-
P.desaturate(n, g(t, c))
47+
P.desaturate(n, getColor(t, c))
4448
/**
4549
* Saturate a color by an amount 0–1
4650
*/
4751
export const saturate = (c: string, n: number) => (t: Theme) =>
48-
P.saturate(n, g(t, c))
52+
P.saturate(n, getColor(t, c))
4953

5054
/**
5155
* Shade a color by an amount 0–1
5256
*/
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))
5459
/**
5560
* Tint a color by an amount 0–1
5661
*/
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))
5864

5965
export const transparentize = (c: string, n: number) => (t: Theme) =>
60-
P.transparentize(n, g(t, c))
66+
P.transparentize(n, getColor(t, c))
6167
/**
6268
* Set the transparency of a color to an amount 0-1
6369
*/
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)
6572

6673
/**
6774
* Mix two colors by a specific ratio
6875
*/
6976
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))
7178

7279
/**
7380
* Get the complement of a color
7481
*/
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))
7684

7785
/**
7886
* Get the inverted color
7987
*/
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))
8189

8290
/**
8391
* Desaturate the color to grayscale

0 commit comments

Comments
 (0)