Skip to content

Commit 330a991

Browse files
committed
Allow palette size to be customisable
1 parent 1568777 commit 330a991

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

src/app/components/FrostedGlassPromo/FrostedGlassPanel.jsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,17 @@ const Children = styled.div`
5151
`}
5252
`;
5353

54-
const FrostedGlassPanel = ({ image, children, minimumContrast }) => {
54+
const FrostedGlassPanel = ({
55+
image,
56+
children,
57+
minimumContrast,
58+
paletteSize,
59+
}) => {
5560
const { isLoading, colour } = useImageColour(image, {
5661
fallbackColour: FALLBACK_COLOUR,
5762
minimumContrast,
5863
contrastColour: '#ffffff',
64+
paletteSize,
5965
});
6066

6167
return (
@@ -72,10 +78,12 @@ FrostedGlassPanel.propTypes = {
7278
image: string.isRequired,
7379
children: node.isRequired,
7480
minimumContrast: number,
81+
paletteSize: number,
7582
};
7683

7784
FrostedGlassPanel.defaultProps = {
7885
minimumContrast: 8,
86+
paletteSize: 10,
7987
};
8088

8189
export default FrostedGlassPanel;

src/app/components/FrostedGlassPromo/index.jsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ const FrostedGlassPromo = ({
7575
eventTrackingData,
7676
index,
7777
minimumContrast,
78+
paletteSize,
7879
}) => {
7980
const { script, service } = useContext(ServiceContext);
8081

@@ -117,6 +118,7 @@ const FrostedGlassPromo = ({
117118
<FrostedGlassPanel
118119
image={image.smallSrc || image.src}
119120
minimumContrast={minimumContrast}
121+
paletteSize={paletteSize}
120122
>
121123
<H3>
122124
<A script={script} service={service} href={url} onClick={onClick}>
@@ -136,6 +138,7 @@ FrostedGlassPromo.propTypes = {
136138
eventTrackingData: shape({}),
137139
index: number,
138140
minimumContrast: number,
141+
paletteSize: number,
139142
image: shape({
140143
src: string.isRequired,
141144
alt: string.isRequired,
@@ -153,6 +156,7 @@ FrostedGlassPromo.defaultProps = {
153156
eventTrackingData: null,
154157
index: 0,
155158
minimumContrast: 8,
159+
paletteSize: 10,
156160
};
157161

158162
export default withData(FrostedGlassPromo);

src/app/components/FrostedGlassPromo/index.stories.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ const Component = props => {
3636
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean quam magna, lacinia ut arcu in, vulputate ultricies lectus. Vestibulum purus ligula, finibus vel ultrices in, pretium non neque. Sed mauris ante, mollis ac metus fermentum, vestibulum malesuada felis. Nullam a congue mauris. Nulla venenatis felis ac eleifend rutrum.',
3737
);
3838
const minimumContrast = number('Minimum Contrast', 8);
39+
const paletteSize = number('Palette Size', 20, { min: 2, max: 99 });
3940
return (
4041
<Wrappers {...props}>
4142
<Promo
4243
image={{ src: imageUrl, alt: '', width: 500, height: 250, ratio: 52 }}
4344
url="#"
4445
minimumContrast={minimumContrast}
46+
paletteSize={paletteSize}
4547
>
4648
{mainBody}
4749
</Promo>

src/app/hooks/useImageColour/index.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const useImageColour = (
99
fallbackColour = '#000000',
1010
minimumContrast = 0,
1111
contrastColour = '#ffffff',
12+
paletteSize = 10,
1213
} = {},
1314
) => {
1415
const [palette, setPalette] = useState(null);
@@ -20,17 +21,14 @@ const useImageColour = (
2021
setIsLoading(false);
2122
};
2223

23-
// We extract 10 colours to give us more opportunity to find the most vibrant one
24-
const QUANTITY_OF_COLOURS_TO_EXTRACT = 10;
25-
2624
useEffect(() => {
2725
try {
2826
setIsLoading(true);
2927

3028
const colorThief = new ColorThief();
3129
const img = new Image();
3230
img.addEventListener('load', () => {
33-
setPalette(colorThief.getPalette(img, QUANTITY_OF_COLOURS_TO_EXTRACT));
31+
setPalette(colorThief.getPalette(img, paletteSize));
3432
setIsLoading(false);
3533
setError(null);
3634
});
@@ -41,7 +39,7 @@ const useImageColour = (
4139
} catch (err) {
4240
setErrorState();
4341
}
44-
}, [url]);
42+
}, [url, paletteSize]);
4543

4644
return {
4745
colour: selectColour({

0 commit comments

Comments
 (0)