Hi, I've found a bug that when opacity value is really small (like 1% - 4%) function will generate wrong hex.
Expected: "#FF700003"
Received: "#FF70003"
32 | it('returns proper values', () => {
33 | expect(opacity('#FF7000', 0.0)).toBe('#FF700000')
> 34 | expect(opacity('#FF7000', 0.01)).toBe('#FF700003')
if (opacity === 0) color += '00'
else if (opacity !== 1) color += Math.round(opacity * 255).toString(16)
return `#${color}`
const hexOpacity = Math.round(percentage * 255)
.toString(16)
.padStart(2, '0')
.toUpperCase()
return `#${color}${hexOpacity}`
This adds zeros at the beginning if generated opacity doesn't contain 2 characters. This also works for 0.0 value so no if
needed.