-
Notifications
You must be signed in to change notification settings - Fork 671
/
index.ts
93 lines (84 loc) · 2.43 KB
/
index.ts
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
import * as P from 'polished'
import { get, Theme } from '@theme-ui/css'
/**
* Get color from theme.colors
*/
export const getColor = (theme: Theme, color: string) =>
get(theme, `colors.${color}`, color)
.replace(/^var\(--(\w+)(.*?), /, '')
.replace(/\)/, '')
/**
* Darken a color by an amount 0–1
*/
export const darken = (c: string, n: number) => (t: Theme) =>
P.darken(n, getColor(t, c))
/**
* Lighten a color by an amount 0–1
*/
export const lighten = (c: string, n: number) => (t: Theme) =>
P.lighten(n, getColor(t, c))
/**
* Rotate the hue of a color by an amount 0–360
*/
export const rotate = (c: string, d: number) => (t: Theme) =>
P.adjustHue(d, getColor(t, c))
/**
* Set the hue of a color to a degree 0–360
*/
export const hue = (c: string, h: number) => (t: Theme) =>
P.setHue(h, getColor(t, c))
/**
* Set the saturation of a color to an amount 0–1
*/
export const saturation = (c: string, s: number) => (t: Theme) =>
P.setSaturation(s, getColor(t, c))
/**
* Set the lightness of a color to an amount 0–1
*/
export const lightness = (c: string, l: number) => (t: Theme) =>
P.setLightness(l, getColor(t, c))
/**
* Desaturate a color by an amount 0–1
*/
export const desaturate = (c: string, n: number) => (t: Theme) =>
P.desaturate(n, getColor(t, c))
/**
* Saturate a color by an amount 0–1
*/
export const saturate = (c: string, n: number) => (t: Theme) =>
P.saturate(n, getColor(t, c))
/**
* Shade a color by an amount 0–1
*/
export const shade = (c: string, n: number) => (t: Theme) =>
P.shade(n, getColor(t, c))
/**
* Tint a color by an amount 0–1
*/
export const tint = (c: string, n: number) => (t: Theme) =>
P.tint(n, getColor(t, c))
export const transparentize = (c: string, n: number) => (t: Theme) =>
P.transparentize(n, getColor(t, c))
/**
* Set the transparency of a color to an amount 0-1
*/
export const alpha = (c: string, n: number) => (t: Theme) =>
P.rgba(getColor(t, c), n)
/**
* Mix two colors by a specific ratio
*/
export const mix = (a: string, b: string, n = 0.5) => (t: Theme) =>
P.mix(n, getColor(t, a), getColor(t, b))
/**
* Get the complement of a color
*/
export const complement = (c: string) => (t: Theme) =>
P.complement(getColor(t, c))
/**
* Get the inverted color
*/
export const invert = (c: string) => (t: Theme) => P.invert(getColor(t, c))
/**
* Desaturate the color to grayscale
*/
export const grayscale = (c: string) => desaturate(c, 1)