-
Notifications
You must be signed in to change notification settings - Fork 122
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
refactor(xy): drop triple equals true on booleans #1350
refactor(xy): drop triple equals true on booleans #1350
Conversation
f40d049
to
a095403
Compare
Please feel free to close this, make fun of it, tell me it sucks, etc! hahahaaa @monfera |
@@ -142,6 +142,12 @@ export function colorIsDark(color: Color): boolean { | |||
return luminance < 0.2; | |||
} | |||
|
|||
const colors = (tr: any) => (tg: any) => (tb: any) => ({ |
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.
Good idea to abstract out this util indeed! In this case there's no current or foreseeable partial application per color channel, so it could be a single parameter list, and the symbols can simplify to r, g, b as the t prefix carries no meaning in the scope of this function
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.
Ah, that makes sense.
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.
This function is basically a way to invert color channels.
There was also another, definitely simplifiable, piece of code:
? to === undefined
? `rgb(${255 - tr}, ${255 - tg}, ${255 - tb})`
: `rgba(${255 - tr}, ${255 - tg}, ${255 - tb}, ${to})`
that just create an rgb
or an rgba
color string depending if the opacity is defined or not.
I think we can easily replace this by always rendering rgba
strings (non rgbA
😉) where the opacity is always 1
if the param is undefined
, something along those lines:
function invertColor(r:RGB, g:RGB, b:RGB, o = 1) {
return `rgba(${255 - r}, ${255 -g},${255 - b},${o})`;
}
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.
Wow, that's waaaaaay more simple!
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.
Full disclosure, I had no idea of the context of all this rgb "stuff" lol
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.
Ok, I'm going to push what I have so far, but I'm also going to go back through and change it to have this invertColor
fn. I really like the simplicity.
Hi @wayneseymour thanks for the contrib! There's an eslint issue: we recently got rid of husky (blame me), so the code autoformatting on commit went away, we pretty much lean on our editors linking to the ever current eslint rules, not just for the red squigglies but also for reformatting on autosave: The "Semantic Pull Request" check was red as it enforces commitizen rules, in this case,
It's late and I haven't checked the actual logic, but it looks like at least the 3rd visual regression test may have picked up a logic change, worth checking, as here the outcomes are expected to remain the same. Glad to get you started on ways to run the VRT locally for a sometimes faster turnaround |
@@ -155,27 +161,29 @@ export function getTextColorIfTextInvertible( | |||
): Color { | |||
const inverseForContrast = specifiedTextColorIsDark === backgroundIsDark; | |||
const { r: tr, g: tg, b: tb, opacity: to } = stringToRGB(textColor); | |||
if (!textContrast) { |
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.
You know, something about all the conditionals in this fn, are driving me nuts.
It's probably purely academic, but the fact that it checks for truthy
against
inverseForContrast
three times keeps bothering me.
Again, not worth it, but it flows terribly in my eyes, just a personal style nit though.
Not worth upsetting anyone. I did the same code elsewhere in Kibana, and it still bothers me.
I just didnt feel like writing my own cond
fn lol.
Cond
A fn takes a list of predicate fns and then executes different fns, based on the predicates passed in. I'm not even sure how best to impl that. Interesting thought exercise though.
Kinda like: https://ramdajs.com/docs/#cond
I'm just venting emotionally though hahahahaaa
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.
What I did is just as "unattractive": https://github.com/elastic/kibana/blob/master/src/dev/code_coverage/ingest_coverage/either.js#L49
jenkins test this |
@@ -142,6 +142,10 @@ export function colorIsDark(color: Color): boolean { | |||
return luminance < 0.2; | |||
} | |||
|
|||
const reducer = (acc: any, curr: any, idx: any) => (idx !== 0 ? `${acc}, ${curr}` : `${curr}`); | |||
|
|||
const invert = (r: number, g: number, b: number) => [r, g, b].map((x) => 255 - x).reduce(reducer, ''); |
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.
[].join(', ')
removes the need for the [].reduce
and the overly generically named reducer
, though even with that, the mild repetition
const invert = (r: number, g: number, b: number) => `${255 - r}, ${255 - g}, ${255 - b}`
would likely lead to fewer cycles, fewer bytes allocated and a bit shorter code, plus the code reader sees what's going on on a 1st blink. I wouldn't name it invert
though as it returns a partial CSS color string, besides the inversion.
Combined with Marko's pitch for using rgba
even if rgb
would suffice, it could be
const invertedColor =
(r: number, g: number, b: number, a: number) => `rgba(${255 - r}, ${255 - g}, ${255 - b}, a)`
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.
Ahhh, ok. That makes a lot of sense. I'll update.
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.
Does that mean rgb
and rgbA
are nearly interchangeable?
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.
Ahh, I see, it's about opacity. I get it now.
jenkins test this |
Refactor to use truthy, remove a little duplication using native bind method. Signed-off-by: Tre' Seymour <[email protected]>
with ternary expression. Less code is the best code :)
parameter list. Also, remove partial application.
Use Marco's inversion fn instead, also lifted. Make it a little DRY-er.
I'm not super happy with this commit. I've considered binding the makeHighContrastColor fn, and partially applying it, but the amount of code that takes seems not worth it. This way, at least it's only evaluated if textContrast is a number. We could also lift this out of this fn, and instead of using hoisting, it'd be truly first-class, but then the parameter list would be huge. Lots of pros and cons, but the vrt is happy!
@wayneseymour the resolution for the
I did this conversion to ternary when we looked at axis code together, but then I also introduced |
Running the vrt now on my latest changes, if it passes, I'll push |
4219fbd
to
d59bdd2
Compare
: makeHighContrastColor(textColor, backgroundColor, textContrast); | ||
? makeHighContrastColor(`rgb(${inverted})`, backgroundColor, ratio || undefined) | ||
: makeHighContrastColor(`rgba(${inverted}, ${to})`, backgroundColor, ratio || undefined) | ||
: makeHighContrastColor(textColor, backgroundColor, ratio || undefined); |
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.
Now that I think about it, this could be simply , ratio
...should be undefined if it's not passed.
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.
@monfera thoughts?
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 can explain my way why we should refactor this function in a different way:
- calling
makeHighContrastColor
with an rgba or rgb color is exactly the same. The internal code take care of the values. - we can always write an
rgba
color starting from anrgb
color adding opacity 1 - the
ratio
or is defined and a number if not it should be left undefined living themakeHighContrastColor
use the default value
export function getTextColorIfTextInvertible(
textColorIsDark: boolean,
backgroundIsDark: boolean,
textColor: Color,
textContrast: TextContrast,
backgroundColor: Color,
): Color {
const finalTextColor = textColorIsDark === backgroundIsDark ? invertColor(stringToRGB(textColor)) : textColor;
const contrastRatio = typeof textContrast === 'number' ? textContrast : undefined;
return textContrast === false
? finalTextColor
: makeHighContrastColor(finalTextColor, backgroundColor, contrastRatio);
}
function invertColor({ r, g, b, opacity }: RgbObject): Color {
return `rgba(${255 - r}, ${255 - g}, ${255 - b}, opacity: ${opacity})`;
}
I have some doubts on the real need of this function anyway, but if we extract it out of the context this is my solution
jenkins test this |
jenkins test this |
Summary
Refactor
textContrast
check to usetruthy
,instead of using
=== true
.Details
Remove a little duplication,
while in the "neighbourhood".
Issues
Helps with: #1303
check for === true, === false, return true etc. redundancy candidates and eliminate