Skip to content

Migrate postcss-lab-function #13

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

Merged

Conversation

romainmenke
Copy link
Member

@romainmenke romainmenke commented Nov 19, 2021

  • converted to typescript
  • inlined the color conversion
  • added unit tests for the color conversion

inlined the color conversion

The current color conversion is not perfect, neither is the conversion in https://github.com/csstools/postcss-lab-function

This conversion tries to match https://facelessuser.github.io/coloraide/
As this packages was suggested as a reference in current spec work.
They try to implement the CSS spec in python.

Once out of gamut color handling is fully specced we should rework the color conversion.
Inlining the functions makes it possible to do this locally without needing to introduce breaking changes in https://www.npmjs.com/package/@csstools/convert-colors

Ideally convert-colors would one day be brought up to speed too.
But I want to avoid getting stuck here because that package is much bigger than what we require for lab and lch


fixes lch hue values with units : csstools/postcss-lab-function#12

This was a small fix.
Just added a check for the unit of the last value and the conversion for the value.


fixes CSS variables : csstools/postcss-lab-function#11

:root {
  --firebrick-a50-var: lch(40% 68.8 34.5 / var(--opacity-50));
}
:root {
  --firebrick-a50-var: rgba(179, 34, 35, var(--opacity-50));
}

@supports (color: lab(0% 0 0)) and (color: lch(0% 0 0)) {
  :root {
    --firebrick-a50-var: lch(40% 68.8 34.5 / var(--opacity-50));
  }
}

fixes rounding of rgb values : csstools/postcss-lab-function#10

I tested this in browsers and they also round the rgb() values.

color: rgb(1.123, 32.234, 5.50)

window.getComputedStyle(foo).color -> rgb(1, 32, 6)

@romainmenke romainmenke changed the title postcss-lab-function Example of extensive rework (postcss-lab-function) Nov 20, 2021
@romainmenke romainmenke marked this pull request as ready for review November 28, 2021 11:20
@romainmenke romainmenke changed the title Example of extensive rework (postcss-lab-function) Migrate postcss-lab-function Nov 28, 2021
@Antonio-Laguna
Copy link
Member

Does this round up or down? Floor or ceil? I'm worried about the tests updates

@romainmenke
Copy link
Member Author

Does this round up or down? Floor or ceil? I'm worried about the tests updates

It uses Math.round based on what browsers currently do.

document.documentElement.style="color: rgb(0, 0, 0.49)";
console.log(window.getComputedStyle(document.documentElement).color);
// rgb(0, 0, 0)
console.log(Math.round(0.49));
// 0

document.documentElement.style="color: rgb(0, 0, 0.50)";
console.log(window.getComputedStyle(document.documentElement).color);
// rgb(0, 0, 1)
console.log(Math.round(0.50));
// 1

document.documentElement.style="color: rgb(0, 0, 0.51)";
console.log(window.getComputedStyle(document.documentElement).color);
// rgb(0, 0, 1)
console.log(Math.round(0.51));
// 1

Copy link
Member

@Antonio-Laguna Antonio-Laguna left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! This is very thorough

@Antonio-Laguna Antonio-Laguna merged commit a5a2fed into main Nov 28, 2021
@Antonio-Laguna Antonio-Laguna deleted the postcss-lab-function--passionate-finnish-spitz-a43b5b5214 branch November 28, 2021 11:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants