Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[system] Fix parsing of hsla colors in getLuminance #34437

Merged
merged 2 commits into from Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/mui-system/src/colorManipulator.js
Expand Up @@ -189,7 +189,10 @@ export function hslToRgb(color) {
export function getLuminance(color) {
color = decomposeColor(color);

let rgb = color.type === 'hsl' ? decomposeColor(hslToRgb(color)).values : color.values;
let rgb =
color.type === 'hsl' || color.type === 'hsla'
? decomposeColor(hslToRgb(color)).values
: color.values;
rgb = rgb.map((val) => {
if (color.type !== 'color') {
val /= 255; // normalized
Expand Down
12 changes: 12 additions & 0 deletions packages/mui-system/src/colorManipulator.test.js
Expand Up @@ -199,6 +199,14 @@ describe('utils/colorManipulator', () => {
expect(getLuminance('rgb(255, 255, 255)')).to.equal(1);
});

it('returns a valid luminance for hsla black', () => {
expect(getLuminance('hsla(0, 100%, 0%, 1)')).to.equal(0);
});

it('returns a valid luminance for hsla white', () => {
expect(getLuminance('hsla(0, 100%, 100%, 1)')).to.equal(1);
});

it('returns a valid luminance for rgb mid-grey', () => {
expect(getLuminance('rgba(127, 127, 127)')).to.equal(0.212);
expect(getLuminance('rgb(127, 127, 127)')).to.equal(0.212);
Expand All @@ -212,6 +220,10 @@ describe('utils/colorManipulator', () => {
expect(getLuminance('hsl(100, 100%, 50%)')).to.equal(0.735);
});

it('returns a valid luminance from an hsla color', () => {
expect(getLuminance('hsla(100, 100%, 50%, 1)')).to.equal(0.735);
});

it('returns an equal luminance for the same color in different formats', () => {
const hsl = 'hsl(100, 100%, 50%)';
const rgb = 'rgb(85, 255, 0)';
Expand Down