Skip to content

Commit 967ba86

Browse files
authored
Merge pull request #441 from colebemis/patch-1
[@theme-ui/colors] Implement alpha color function
2 parents a4f4c43 + 3171881 commit 967ba86

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

packages/color/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ import { tint } from '@theme-ui/color'
116116
// tint('primary', amount)
117117
```
118118

119+
### `alpha`
120+
121+
Set the transparency of a color to an amount 0-1
122+
123+
```js
124+
125+
import { alpha } from '@theme-ui/color'
126+
// alpha('primary', amount)
127+
```
128+
119129
### `mix`
120130

121131
Mix two colors by a specific ratio

packages/color/src/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ export const saturate = (c, n) => t => P.saturate(n, g(t, c))
1717
export const shade = (c, n) => t => P.shade(n, g(t, c))
1818
export const tint = (c, n) => t => P.tint(n, g(t, c))
1919

20-
// maybe not from polished? opacify??
21-
// export const alpha = (c, n) => t =>
20+
export const alpha = (c, n) => t => P.rgba(g(t, c), n)
2221

2322
export const mix = (a, b, n = 0.5) => t => P.mix(n, g(t, a), g(t, b))
2423

packages/color/test/index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
saturate,
1010
shade,
1111
tint,
12+
alpha,
1213
mix,
1314
complement,
1415
invert,
@@ -72,6 +73,11 @@ test('tint', () => {
7273
expect(n).toBe('#3fd8ff')
7374
})
7475

76+
test('alpha', () => {
77+
const n = alpha('primary', 0.25)(theme)
78+
expect(n).toBe('rgba(0,204,255,0.25)')
79+
})
80+
7581
test('mix', () => {
7682
const n = mix('primary', 'secondary', 0.25)(theme)
7783
expect(n).toBe('#4c59b2')
@@ -149,6 +155,11 @@ test('tintCustomProps', () => {
149155
expect(n).toBe('#3fd8ff')
150156
})
151157

158+
test('alphaCustomProps', () => {
159+
const n = alpha('primary', 0.25)(themeCustomProps)
160+
expect(n).toBe('rgba(0,204,255,0.25)')
161+
})
162+
152163
test('mixCustomProps', () => {
153164
const n = mix('primary', 'secondary', 0.25)(themeCustomProps)
154165
expect(n).toBe('#4c59b2')
@@ -189,4 +200,4 @@ const themeTomatoCustomProps = {
189200
test('darkenTomatoCustomProps', () => {
190201
const n = darken('primary', 0.25)(themeTomatoCustomProps)
191202
expect(n).toBe('#c61e00')
192-
})
203+
})

0 commit comments

Comments
 (0)