Skip to content

Commit 7760d30

Browse files
committed
feat(svg-filter-cc-saturation) Add saturation prop to ColorCorrection
1 parent a94e439 commit 7760d30

1 file changed

Lines changed: 28 additions & 3 deletions

File tree

src/components/svg-filters.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,44 @@ const Percentage = (props, propName, componentName) => {
99
}
1010
}
1111

12+
const RED_LUMINANCE = 0.2126
13+
const GREEN_LUMINANCE = 0.7152
14+
const BLUE_LUMINANCE = 0.0722
15+
1216
/**
1317
* ColorCorrection :: Props -> React.Element
18+
*
19+
* Memo: https://drafts.fxtf.org/filter-effects/#element-attrdef-fecolormatrix-values
1420
*/
15-
const ColorCorrection = ({ lightness: relativeLightness = 1, opacity = 0.5 }) => {
21+
const ColorCorrection = ({ lightness = 1, opacity = 0.5, saturation = 1 }) => {
22+
23+
const RR = RED_LUMINANCE + ((1 - RED_LUMINANCE) * saturation)
24+
const RG = GREEN_LUMINANCE - (GREEN_LUMINANCE * saturation)
25+
const RB = BLUE_LUMINANCE - (BLUE_LUMINANCE * saturation)
26+
27+
const GR = RED_LUMINANCE - (RED_LUMINANCE * saturation)
28+
const GG = GREEN_LUMINANCE + ((1 - GREEN_LUMINANCE) * saturation)
29+
const GB = BLUE_LUMINANCE - (BLUE_LUMINANCE * saturation)
30+
31+
const BR = RED_LUMINANCE - (RED_LUMINANCE * saturation)
32+
const BG = GREEN_LUMINANCE - (GREEN_LUMINANCE * saturation)
33+
const BB = BLUE_LUMINANCE + ((1 - BLUE_LUMINANCE) * saturation)
1634

17-
const lightness = 1 * (1 - relativeLightness)
35+
const LIGHTNESS = 1 * (1 - lightness)
1836

19-
return <feColorMatrix values={`1 0 0 0 ${lightness} 0 1 0 0 ${lightness} 0 0 1 0 ${lightness} 0 0 0 ${opacity} 0`} />
37+
return (
38+
<feColorMatrix values={`
39+
${RR} ${RG} ${RB} ${LIGHTNESS} 0
40+
${GR} ${GG} ${GB} ${LIGHTNESS} 0
41+
${BR} ${BG} ${BB} ${LIGHTNESS} 0
42+
0 0 0 ${opacity} 0`} />
43+
)
2044
}
2145

2246
ColorCorrection.propTypes = {
2347
lightness: NumberOrString,
2448
opacity: NumberOrString,
49+
saturation: NumberOrString,
2550
}
2651

2752
/**

0 commit comments

Comments
 (0)