Skip to content

Commit 94a3313

Browse files
Inbal-Tishethanshar
authored andcommitted
Add saturation adjustment for generated color palette (#491)
* Add saturation adjustment for generated color palette * adding tests * shorting method
1 parent df2aa1c commit 94a3313

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

src/style/__tests__/colors.spec.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ describe('style/Colors', () => {
108108
expect(cachedPalette).toBeDefined();
109109
expect(cachedPalette.length).toBe(8);
110110
expect(cachedPalette.includes('#3F88C5')).toBe(true);
111-
})
111+
});
112+
113+
it('should generateColorPalette', () => {
114+
const palette = uut.generateColorPalette('#3F88C5');
115+
expect(palette).toEqual(['#193852', '#255379', '#316EA1', '#3F88C5', '#66A0D1', '#8DB9DD', '#B5D1E9', '#DCE9F4']);
116+
});
117+
118+
it('should generateColorPalette with adjusted saturation', () => {
119+
const palette = uut.generateColorPalette('#FFE5FF');
120+
expect(palette).toEqual(['#661A66', '#8F248F', '#B82EB7', '#D148D1', '#DB71DB', '#E699E6', '#F0C2F0', '#FFE5FF']);
121+
});
112122
});
113123
});

src/style/colors.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,34 @@ class Colors {
123123
tints.push(tint);
124124
});
125125

126-
const sliced = tints.slice(0, 8);
127-
return sliced;
126+
let sliced = tints.slice(0, 8);
127+
const adjusted = adjustSaturation(sliced, color);
128+
return adjusted || sliced;
128129
});
129130
}
130131

132+
function adjustSaturation(colors, color) {
133+
let array;
134+
const lightnessLevel = 80;
135+
const saturationLevel = 60;
136+
const hsl = Color(color).hsl();
137+
const lightness = Math.round(hsl.color[2]);
138+
139+
if (lightness > lightnessLevel) {
140+
const saturation = Math.round(hsl.color[1]);
141+
if (saturation > saturationLevel) {
142+
array = _.map(colors, e => e !== color ? addSaturation(e, saturationLevel) : e)
143+
}
144+
}
145+
return array;
146+
}
147+
148+
function addSaturation(color, saturation) {
149+
const hsl = Color(color).hsl();
150+
hsl.color[1] = saturation;
151+
return hsl.hex();
152+
}
153+
131154
function generateColorTint(color, tintLevel) {
132155
const hsl = Color(color).hsl();
133156
hsl.color[2] = tintLevel;

0 commit comments

Comments
 (0)