-
-
Notifications
You must be signed in to change notification settings - Fork 209
/
transparentize.js
47 lines (45 loc) · 1.44 KB
/
transparentize.js
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
// @flow
import rgba from './rgba'
import parseToRgb from './parseToRgb'
import guard from '../internalHelpers/_guard'
import curry from '../internalHelpers/_curry'
/**
* Decreases the opacity of a color. Its range for the amount is between 0 to 1.
*
*
* @example
* // Styles as object usage
* const styles = {
* background: transparentize(0.1, '#fff');
* background: transparentize(0.2, 'hsl(0, 0%, 100%)'),
* background: transparentize(0.5, 'rgba(255, 0, 0, 0.8)'),
* }
*
* // styled-components usage
* const div = styled.div`
* background: ${transparentize(0.1, '#fff')};
* background: ${transparentize(0.2, 'hsl(0, 0%, 100%)')},
* background: ${transparentize(0.5, 'rgba(255, 0, 0, 0.8)')},
* `
*
* // CSS in JS Output
*
* element {
* background: "rgba(255,255,255,0.9)";
* background: "rgba(255,255,255,0.8)";
* background: "rgba(255,0,0,0.3)";
* }
*/
function transparentize(amount: number, color: string) {
const parsedColor = parseToRgb(color)
const alpha: number =
typeof parsedColor.alpha === 'number' ? parsedColor.alpha : 1
const colorWithAlpha = {
...parsedColor,
alpha: guard(0, 1, (alpha * 100 - amount * 100) / 100),
}
return rgba(colorWithAlpha)
}
// Don’t inline this variable into export because Rollup will remove the /*#__PURE__*/ comment
const curriedTransparentize = /*#__PURE__*/ curry(transparentize) // eslint-disable-line spaced-comment
export default curriedTransparentize