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

Allow any color type when calling individual deltaE functions #451

Merged
merged 2 commits into from
Feb 27, 2024

Conversation

lloydk
Copy link
Collaborator

@lloydk lloydk commented Feb 23, 2024

The types for the deltaE functions didn't allow all ColorTypes as parameters. For example, the types didn't allow "red" to be passed as a parameter.

Also, calling a deletaE method with a color such as "red" would cause an exception to be thrown.

let x = deltaE2000("red", "white") // Exception thrown

Copy link

netlify bot commented Feb 23, 2024

Deploy Preview for colorjs ready!

Name Link
🔨 Latest commit b96472a
🔍 Latest deploy log https://app.netlify.com/sites/colorjs/deploys/65dc094c406e8d0008386c2c
😎 Deploy Preview https://deploy-preview-451--colorjs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Member

@MysteryBlokHed MysteryBlokHed left a comment

Choose a reason for hiding this comment

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

TypeScript changes LGTM

@LeaVerou LeaVerou self-requested a review February 24, 2024 18:54
Copy link
Member

@LeaVerou LeaVerou left a comment

Choose a reason for hiding this comment

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

Agree on solving the problem, not sure about this particular solution, I really don't like the repetition. If there are other commonalities between the deltaE functions, we could make this part of a utility function from deltaE.js that these methods import.

If there aren't other commonalities, maybe a better solution is to make getColor() accept a variable number of arguments so it can become a single line:

let [sample, color] = getColor(sample, color);`

@lloydk
Copy link
Collaborator Author

lloydk commented Feb 24, 2024

Agree on solving the problem, not sure about this particular solution, I really don't like the repetition. If there are other commonalities between the deltaE functions, we could make this part of a utility function from deltaE.js that these methods import.

I don't think there are any commonalities between the deltaE functions.

The contrast functions have the same repetition.

export default function contrastAPCA (background, foreground) {
foreground = getColor(foreground);
background = getColor(background);

If there aren't other commonalities, maybe a better solution is to make getColor() accept a variable number of arguments so it can become a single line:

What about adding a getColors function to getColor.js?

export function getColors (...colors) {
	return colors.map(getColor);
}

I don't have a strong opinion on any of the solutions:

  • keep the repetition
  • change getColor to take a variable number of arguments
  • add a getColors function

If we want to make changes to getColor or add a getColors function I can create a separate PR for that change.

@LeaVerou
Copy link
Member

How about this option: instead of variable arguments to getColor() we support an array argument. If an array is passed in, an array is returned.

Pros:

  • Skips the weird discontinuity at 1 -> 2 args
  • Explicit opt in to array handling, possible to handle arrays with even 1 element
  • Allows us to add an options object later on

It does add two extra chars to usage, but I think that's an ok tradeoff, and it makes it more explicit.

The advantage of extending getColor instead of adding a new function is also that this means Color.get() gets this for free.

@lloydk
Copy link
Collaborator Author

lloydk commented Feb 25, 2024

How about this option: instead of variable arguments to getColor() we support an array argument. If an array is passed in, an array is returned.

Pros:

  • Skips the weird discontinuity at 1 -> 2 args
  • Explicit opt in to array handling, possible to handle arrays with even 1 element
  • Allows us to add an options object later on

It does add two extra chars to usage, but I think that's an ok tradeoff, and it makes it more explicit.

The advantage of extending getColor instead of adding a new function is also that this means Color.get() gets this for free.

Created PR #453 for getColor.

When you say Color.get() do you mean this function or am I missing something?

color.js/src/color.js

Lines 111 to 121 in 52d5f69

/**
* Get a color from the argument passed
* Basically gets us the same result as new Color(color) but doesn't clone an existing color object
*/
static get (color, ...args) {
if (color instanceof Color) {
return color;
}
return new Color(color, ...args);
}

@LeaVerou
Copy link
Member

When you say Color.get() do you mean this function or am I missing something?

Ah, I thought it was using getColor. We may want to update that too then to keep them in sync.

The types for the deltaE functions didn't allow all ColorTypes
as parameters. For example, the types didn't allow "red" to be
passed as a parameter.

Also, calling a deletaE method with a color such as "red"
would cause an exception to be thrown.

e.g. let x = deltaE2000("red", "white") // Exception thrown
@lloydk
Copy link
Collaborator Author

lloydk commented Feb 26, 2024

Messed up my branch. Hopefully I can fix it without making more of a mess.

Copy link
Member

@LeaVerou LeaVerou left a comment

Choose a reason for hiding this comment

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

LGTM!

@lloydk lloydk merged commit 35e8f75 into color-js:main Feb 27, 2024
5 checks passed
@lloydk lloydk deleted the delta-e-types branch February 27, 2024 04:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants