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

feat: type safe colour token parser #209

Closed
4 tasks done
prjctimg opened this issue Jul 22, 2024 · 4 comments · Fixed by #201
Closed
4 tasks done

feat: type safe colour token parser #209

prjctimg opened this issue Jul 22, 2024 · 4 comments · Fixed by #201
Assignees
Labels
bug Something isn't working urgent This issue has a direct effect on the user experience of the project.
Milestone

Comments

@prjctimg
Copy link
Owner

prjctimg commented Jul 22, 2024

Inspiration

Previously (v2.x.x and below) the library exported different functions to convert colours from different types to a common format like hex.

However, v3 is focused on a more simplified API and therefore all the converter functions have been merged into one utility function token()

How does it work

The function will be broken down into several steps:

  • Parsing
  • Conversion
  • Output modification
  • RGB normalization

Parsing

Because we are expecting colors in different types we first have to parse our colour to a widely accepted format. This helps ensure that we don't branch off our logic for no reason.

When an array is passed as a colour token it is converted to a colour object so that it can be passed to functions like formatHex or formatHex8 in the specified srcMode. The same can be said to all the other types which will be converted to plain objects as well.

Conversion

If the targetMode option is specified and the kind is set to 'arr' or 'obj' the colour will be converted to the specified colour space. This is also where we perform normalizeRGB which will allow us to make [0,255] RGB channel ranges compatible with the [0,1] range supported by Culori.

Output modification

This is the stage where we modify the final result to match the options that may be specified for example

  • Omitting the alpha channel
  • Omitting the colour space mode
  • Specifying the output type

This is a core function because it allows the parsing of any recognisable colour tokens throughout the library.

@prjctimg prjctimg added bug Something isn't working urgent This issue has a direct effect on the user experience of the project. labels Jul 22, 2024
@prjctimg prjctimg added this to the v3 milestone Jul 22, 2024
@prjctimg prjctimg self-assigned this Jul 22, 2024
Copy link

Thanks for the issue, our team will look into it as soon as possible! If you would like to work on this issue, please wait for a maintainer to decide if it is ready to be worked on.

To claim an issue, please leave a comment that says ".take". If you have any questions, feel free to ping a maintainer.

@prjctimg prjctimg pinned this issue Jul 22, 2024
@prjctimg prjctimg linked a pull request Jul 22, 2024 that will close this issue
prjctimg added a commit that referenced this issue Jul 22, 2024
@prjctimg
Copy link
Owner Author

Default color spaces

When parsing our colors we may want to omit properties like colorspace for example, when working with arrays.

The srcMode is 'rgb' by default and the targetMode is undefined by default as well. In this scenario, the targetMode defaults to the srcMode when 'kind' is 'obj' | 'arr' to avoid errors by passing undefined values to the converter

@prjctimg
Copy link
Owner Author

#211

Token parser now works as expected

@prjctimg prjctimg reopened this Aug 25, 2024
@prjctimg
Copy link
Owner Author

prjctimg commented Aug 25, 2024

The other bug I'm facing is that channel values are being normalized like rgb even when the mode is not RGB. Working on it right now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working urgent This issue has a direct effect on the user experience of the project.
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

1 participant