-
Notifications
You must be signed in to change notification settings - Fork 82
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
Audit function parameter and return types (2/2) #457
Conversation
Ensure that functions in the following modules have the correct types and ensure that all color types can be passed as parameters: - inGamut - luminance - set - setAll - space - toGamut
✅ Deploy Preview for colorjs ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
I wasn't 100% convinced that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't sure about the types for the
to
andfrom
methods onColorspace
.
If the circular dependency doesn't hurt anything in practice, I'd lean toward calling getColor
there to be consistent.
I wasn't 100% convinced that
getColor
should be called insetAll
but that's what I've gone with for now.
I'm not opposed to calling getColor
in set/setAll etc, but currently there are a number of types that are incorrect, some of them a bit tangential to this PR.
First, the type of getColor
itself is wrong -- it claims to return a PlainColorObject
(or PlainColorObject[]
), where in reality if it is passed a Color
object, it returns a Color
object. That means that every fn which calls getColor
and then returns the response (e.g. set
, setAll
, and toGamut
) is also wrong -- they all claim to return a PlainColorObject
but often return a Color
.
Honestly, this (that is, keeping the types up-to-date with the codebase) is going to be an ongoing headache unless we're willing to start re-writing the code itself in TypeScript. That's something I'd be willing to consider taking on (potentially with help from @MysteryBlokHed, if he's willing), but I vaguely recall that @LeaVerou was opposed to that direction -- Lea, can you confirm?
[Edit: This TypeScript question should not hold up progress on this PR, and fixing the incorrect types.]
Keep in mind that The types for the let x: PlainColorObject = getColor("red"); // returns an object literal
let y: PlainColorObject = getColor(new Color("red")); // returns a Color object
let z: Color = getColor(new Color("red")); // type error If we want to make additional guarantees about returning the same object if it conforms to the None of the functions exported in 'colorjs.io/fn' should have types that reference the |
I do have some reservations, one of which being that there are many folks who are proficient in color science but only dabble with JS, and a TS codebase is hostile to them. Whereas having the types separately means we can still accept contributions from folks who are not comfortable with TS, and adjust the typings ourselves after. I’m willing to examine it , but could you elaborate a bit on how TS would help with this issue? I obviously get how it would help us keep types up to date, but not how it would help with the return type issue. My understanding is that TS does quite poorly with very dynamic APIs where functions are computed from other functions etc. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep in mind that
PlainColorObject
is an interface and theColor
type implements thePlainColorObject
interface.None of the functions exported in 'colorjs.io/fn' should have types that reference the
Color
type because theColor
class isn't available incolorjs.io/fn
.
@lloydk Thanks -- those are both really helpful points, and I was missing the forest for the trees. 🤦
I've pushed some proposed changes/additions for you to review in lloydk#1, mostly to see what it looks like if we enforce more consistent Color
object return types from the Color namespace methods.
could you elaborate a bit on how TS would help with this issue? I obviously get how it would help us keep types up to date, but not how it would help with the return type issue.
@LeaVerou I'm distracting from the purpose of this PR, so I'll open a new issue to consider this question. 😄
My understanding is that TS does quite poorly with very dynamic APIs where functions are computed from other functions etc.
Yes, that can be true in some cases. I may try it out on a few methods and see what it looks like.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine merging this as-is, then I have a follow-up PR I can open with a few more proposed changes.
Did you still want me to review lloydk#1 or should I wait for the follow-up PR? |
Probably easier for me to open the PR here and review it then -- thanks! |
* main: Audit function parameter and return types (2/2) (#457) Always show delta display to help confirm it is in gamut (#458) Final clip is required in Ray tracing [apps/gamut-mapping] Add edge seeker algorithm (#448) Audit function parameter and return types (1/2) (#456) Rework raytracing limiting hue shift to no more than 3 degrees at worst
Ensure that functions in the following modules have the correct types and ensure that all color types can be passed as parameters:
space.js
I wasn't sure about the types for the
to
andfrom
methods onColorspace
. If we allow any color type and callgetColor
in those methods then we introduce a cyclic dependency between thegetColor
module and thespace
module. If we're ok having the cyclic dependency and want to allow any color type I can make the appropriate changes.