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(Color) Regex and color code cleanup and new color format support #8916

Merged
merged 33 commits into from May 18, 2023

Conversation

asturur
Copy link
Member

@asturur asturur commented May 13, 2023

Motivation

Improve the regexes and the documentation around them.

So the part with strings and composition was definitely the most readable.
But took 400 bytes of extra code where usually we fit a small feature or a fix or 2.
Now we don't read regexes as a flow, we want to be able to know what they do and i wrote extensive docs for them

Changes

Following

https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/rgb
and
https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/hsl

  • improve the regex by adding the ability to parse the alpha channel from the format 80%
  • add the ability to recognize the format with spaces and not commas and the / for the alpha channel
  • add comments, lot of them

Gist

In Action

@github-actions
Copy link
Contributor

github-actions bot commented May 13, 2023

Build Stats

file / KB (diff) bundled minified
fabric 919.369 (+4.151) 303.564 (-0.512)

@github-actions
Copy link
Contributor

github-actions bot commented May 13, 2023

Coverage after merging regex-color-cleanup into master will be

83.65%

Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
index.node.ts47.83%100%25%60%17, 20, 23, 40, 43, 46
src
   ClassRegistry.ts100%100%100%100%
   Collection.ts94.71%94.64%86.67%97.09%101, 104, 207–208, 233–234
   CommonMethods.ts96.55%87.50%100%100%10
   Intersection.ts100%100%100%100%
   Observable.ts87.23%85.29%84.62%89.36%144–145, 170–171, 39–40, 48, 57, 91, 99
   Point.ts100%100%100%100%
   Shadow.ts98.36%95.65%100%100%178
   cache.ts97.06%90%100%100%57
   config.ts75%66.67%66.67%82.76%130, 138, 138, 138, 138, 138–140, 151–153
   constants.ts100%100%100%100%
src/Pattern
   Pattern.ts92.21%91.89%90%93.33%116, 127, 136, 29, 92
src/brushes
   BaseBrush.ts100%100%100%100%
   CircleBrush.ts0%0%0%0%107, 107, 107, 109, 111, 113–115, 117–120, 127–128, 135, 137, 22–23, 31–35, 39–43, 50–53, 61–65, 67, 75, 75, 75, 75, 75–76, 78, 78, 78–81, 83, 91–92, 94, 96–98
   PatternBrush.ts97.06%87.50%100%100%21
   PencilBrush.ts91.01%82.35%100%93.75%122–123, 152, 152–154, 176, 176, 276, 280, 285–286, 68–69, 84–85
   SprayBrush.ts0%0%0%0%106, 106, 106, 106, 106–107, 109–110, 117–118, 120, 122–126, 135, 139–140, 140, 148, 148, 148–151, 153–156, 16, 160–161, 163, 165–168, 17, 171, 178–179, 181, 183–184, 186, 19, 193–194, 196–197, 20, 200, 200, 207, 207, 21, 21, 21, 211, 22–23, 27, 36, 43, 50, 57, 64, 83–85, 93–95, 97–98
src/canvas
   Canvas.ts78.87%77.54%81.67%79.41%1001–1002, 1002, 1002–1004, 1006–1007, 1007, 1007, 1009, 1017, 1017, 1017–1019, 1019, 1019, 1025–1026, 1034–1035, 1035, 1035–1036, 1041, 1043, 1074–1076, 1079–1080, 1084–1085, 1198–1200, 1203–1204, 1277, 1396, 1519, 1589, 162, 187, 297–298, 301–305, 310, 333–334, 339–344, 364, 364, 364–365, 365, 365–366, 37, 374, 379–380, 380, 380–381, 383, 392, 398–399, 399, 399, 41, 442, 450, 454, 454, 454–455, 457, 539–540, 540, 540–541, 547, 547, 547–549, 569, 571, 571, 571–572, 572, 572, 575, 575, 575–576, 579, 588–589, 591–592, 594, 594–595, 597–598, 610–611, 611, 611–612, 614–619, 625, 632, 669, 669, 669, 671, 673–678, 684, 690, 690, 690–691, 693, 696, 701, 714, 742, 742, 800–801, 801, 801–802, 804, 807–808, 808, 808–809, 811–812, 815, 815–817, 820–821, 891, 903, 910, 931, 963, 984–985
   SelectableCanvas.ts94.39%91.16%94.64%96.62%1119, 1119–1120, 1123, 1143, 1143, 1201, 1254–1255, 1276, 1284, 1409, 1411, 1413–1414, 518, 698–699, 701–702, 702, 702, 751–752, 813–814, 867–869, 901, 906–907, 934–935
   StaticCanvas.ts96.86%92.91%100%98.61%1102–1103, 1103, 1103–1104, 1224, 1234, 1288–1289, 1292, 1327–1328, 1405, 1414, 1414, 1418, 1418, 1465–1466, 310–311, 328, 759, 771–772
   TextEditingManager.ts100%100%100%100%
src/color
   Color.ts94.96%91.67%96.30%96.05%233, 258–259, 267–268, 48
   color_map.ts100%100%100%100%
   constants.ts100%100%100%100%
   util.ts85.71%76.92%100%89.74%55–56, 56, 58, 58, 58–59, 61–62, 89
src/controls
   Control.ts93.90%88.89%90.91%97.73%232, 319, 319, 354
   changeWidth.ts100%100%100%100%
   commonControls.ts100%100%100%100%
   controlRendering.ts81.63%78%100%84.78%106, 111, 121, 121, 45, 50, 61, 61, 65–72, 81–82
   drag.ts100%100%100%100%
   fireEvent.ts88.89%75%100%100%13
   polyControl.ts5.97%0%0%11.11%100, 105, 119, 119, 119, 119, 119, 121–124, 124, 127, 134, 17, 25–29, 29, 29, 29, 29, 29, 29, 29, 50–56, 56, 56, 56, 56, 58, 63–64, 66, 76, 82–83, 83, 83–84, 88–90, 90, 90, 90, 90, 92
   rotate.ts19.57%12.50%50%21.43%41, 45, 51, 51, 51–52, 55–57, 59, 59, 59, 59, 59–61, 61, 61–63, 65, 65, 65–67, 67, 67–68, 73, 73, 73–74, 76, 78, 80–81
   scale.ts93.57%92.94%100%93.67%129–130, 132–134, 148–149, 181–183, 42
   scaleSkew.ts78.79%64.29%100%85.71%27, 29, 29, 29, 31, 33, 35
   skew.ts91.03%79.31%100%97.67%130–131, 162–163, 170, 176, 178
   util.ts100%100%100%100%
   wrapWithFireEvent.ts100%100%100%100%
   wrapWithFixedAnchor.ts100%100%100%100%
src/env
   browser.ts84.21%77.78%50%100%14, 17
   index.ts100%100%100%100%
   node.ts74.07%33.33%66.67%88.89%27, 31–32, 32, 32, 37
src/filters
   BaseFilter.ts21.62%23.21%32%18.27%100, 100, 100–101, 108–111, 111, 111–112, 118, 118, 118–121, 139, 157, 171–176,

@@ -279,7 +279,7 @@ export class Color {
* @return {TRGBAColorSource | undefined} source
*/
static sourceFromRgb(color: string): TRGBAColorSource | undefined {
const match = color.match(reRGBa);
const match = color.match(new RegExp(reRGBa, 'i'));
Copy link
Member Author

Choose a reason for hiding this comment

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

the truth is that the best code is the literal regex inside the function here.
The constants outside the file are an organization thing that we pay with having those in the scope of the library, always initialized.

@asturur
Copy link
Member Author

asturur commented May 13, 2023

changed my mind.
I ll overcome the readibility with chatgpt aided comments

s = parseFloat(match[2]) / (/%$/.test(match[2]) ? 100 : 1),
l = parseFloat(match[3]) / (/%$/.test(match[3]) ? 100 : 1);
s = parseFloat(match[2]) / 100,
l = parseFloat(match[3]) / 100;
Copy link
Member Author

Choose a reason for hiding this comment

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

s and l are always percentages only.
Non percentages aren't ammitted.

@asturur
Copy link
Member Author

asturur commented May 13, 2023

At this point this needs some extra test for the new format covered

@asturur asturur changed the title chore() Regex color cleanup feat(Color) Regex color cleanup and new color format support May 14, 2023
@asturur asturur changed the title feat(Color) Regex color cleanup and new color format support feat(Color) Regex and color code cleanup and new color format support May 14, 2023
* @return {TRGBColorSource} Hsl color
*/
_rgbToHsl(r: number, g: number, b: number): TRGBColorSource {
_rgbToHsl(r: number, g: number, b: number, a: number): TRGBAColorSource {
Copy link
Member Author

Choose a reason for hiding this comment

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

this should probably be an utility like hsltorgb

@asturur
Copy link
Member Author

asturur commented May 14, 2023

ok i can stop here with this.
Ready to review if some code choice isn't great

@asturur
Copy link
Member Author

asturur commented May 14, 2023

@Lazauya since you had previously worked with regexes, have a look for suggestions

Copy link
Contributor

@ShaMan123 ShaMan123 left a comment

Choose a reason for hiding this comment

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

This PR is a GREAT cleanup!
Some recommended changes

const [r, g, b] = this.getSource();
return `${hexify(r)}${hexify(g)}${hexify(b)}`;
const fullHex = this.toHexa();
return fullHex.slice(0, 6);
Copy link
Contributor

Choose a reason for hiding this comment

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

why isn't it prefixed with #?

Copy link
Member Author

Choose a reason for hiding this comment

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

was like this before, trying to not change behaviour since it doesn't seems important.
I don't know the reasons, we should dig into git history

this.setSource([average, average, average, currentAlpha]);
const [r, g, b, a] = this.getSource(),
average = Math.round(r * 0.3 + g * 0.59 + b * 0.11);
this.setSource([average, average, average, a]);
return this;
Copy link
Contributor

@ShaMan123 ShaMan123 May 15, 2023

Choose a reason for hiding this comment

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

Not very related, but since I am reviewing
Do we want to return a new instance for the toXXX methods?

Copy link
Member Author

Choose a reason for hiding this comment

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

this would be a possible change, and we can do that outside the cleanup/refactor scope.
Something that also should be done maybe that is part of cleanup is consolidathing this average code in a function util

@@ -310,14 +251,14 @@ export class Color {
* @see http://http://www.w3.org/TR/css3-color/#hsl-color
*/
static sourceFromHsl(color: string): TRGBAColorSource | undefined {
const match = color.match(reHSLa);
const match = color.match(reHSLa());
if (!match) {
return;
}

const h = (((parseFloat(match[1]) % 360) + 360) % 360) / 360,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const h = (((parseFloat(match[1]) % 360) + 360) % 360) / 360,
const h = ((parseFloat(match[1]) + 360) % 360) / 360,

Copy link
Member Author

Choose a reason for hiding this comment

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

i wonder... this seems correct and i noticed was off.
I m trying to think of some non obvious value that could break.
But i prefer to discover in this case and in case fix later because this seems wrong

Copy link
Contributor

@ShaMan123 ShaMan123 May 15, 2023

Choose a reason for hiding this comment

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

Ahhh I see.
What if the value is < -360. Now it fails, before it was safe.
Please revert this.
I didn't think of that.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah that is fine, we discovered.

src/color/Color.ts Show resolved Hide resolved
src/color/Color.ts Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

This is great!

Copy link
Member Author

@asturur asturur May 15, 2023

Choose a reason for hiding this comment

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

in my head there was the fun experiment that 150 colors can be represented by 2 digits in base64 encoding. And so that should be possible to reduce this map to have custom hashed keys to which color names resolve univocally. But i m not sure is easy or hard, but for sure would be fun.

Copy link
Contributor

Choose a reason for hiding this comment

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

So desu, ne?!

export const reRGBa =
/^rgba?\(\s*(\d{1,3}(?:\.\d+)?%?)\s*,\s*(\d{1,3}(?:\.\d+)?%?)\s*,\s*(\d{1,3}(?:\.\d+)?%?)\s*(?:\s*,\s*((?:\d*\.?\d+)?)\s*)?\)$/i;
export const reRGBa = () =>
/^rgba?\(\s*(\d{0,3}(?:\.\d+)?%?)\s*[\s|,]\s*(\d{0,3}(?:\.\d+)?%?)\s*[\s|,]\s*(\d{0,3}(?:\.\d+)?%?)\s*(?:\s*[,/]\s*(\d{0,3}(?:\.\d+)?%?)\s*)?\)$/i;
Copy link
Contributor

Choose a reason for hiding this comment

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

does it over complicate reusing some string e.g. \(\s*(\d{0,3}(?:\.\d+)?%?)\s*[\s|,]\s* in a string template?

Copy link
Member Author

Choose a reason for hiding this comment

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

the issue with that is that i loose the ability to use the literal regex and i have to go with newRegexp.
I started with that if you look at the first commits. I got 400 extra bytes of code and i was pissed off.
I should try again now that i made the regex slightly different.

src/color/util.ts Outdated Show resolved Hide resolved
return hexValue.length === 1 ? `0${hexValue}` : hexValue;
}
export const hexify = (value: number) =>
Math.min(value, 255).toString(16).toUpperCase().padStart(2, '0');
Copy link
Contributor

Choose a reason for hiding this comment

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

padStart, who knew. 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

This is exactly where we should verify rounding is not an issue.

Comment on lines +195 to +202
var oColor = fabric.Color.fromRgba(stringToParse);
assert.ok(oColor);
assert.ok(oColor instanceof fabric.Color);
assert.deepEqual(oColor.getSource(), expectedSource);
var oColorUppercase = fabric.Color.fromRgb(stringToParse.toUpperCase());
assert.ok(oColorUppercase);
assert.ok(oColorUppercase instanceof fabric.Color);
assert.deepEqual(oColorUppercase.getSource(), expectedSource);
Copy link
Contributor

Choose a reason for hiding this comment

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

seems this function can be reused in the hsl loop as well

Copy link
Member Author

Choose a reason for hiding this comment

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

could but i didn't becuase fromRgb vs fromHsl and didn't want to overcomplicate it. Still better than all different tests.

Copy link
Contributor

@ShaMan123 ShaMan123 May 15, 2023

Choose a reason for hiding this comment

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

thought of

function runColorTest({ name, stringToParse, expectedSource }, from, fromAlpha) {
      var oColor = fromAlpha(stringToParse);
      assert.ok(oColor);
      assert.ok(oColor instanceof fabric.Color);
      assert.deepEqual(oColor.getSource(), expectedSource);
      var oColorUppercase = from(stringToParse.toUpperCase()); // fromAlpha??
      assert.ok(oColorUppercase);
      assert.ok(oColorUppercase instanceof fabric.Color);
      assert.deepEqual(oColorUppercase.getSource(), expectedSource);
}

asturur and others added 6 commits May 15, 2023 11:56
Co-authored-by: Shachar <34343793+ShaMan123@users.noreply.github.com>
Co-authored-by: Shachar <34343793+ShaMan123@users.noreply.github.com>
Co-authored-by: Shachar <34343793+ShaMan123@users.noreply.github.com>
Co-authored-by: Shachar <34343793+ShaMan123@users.noreply.github.com>
Co-authored-by: Shachar <34343793+ShaMan123@users.noreply.github.com>
Co-authored-by: Shachar <34343793+ShaMan123@users.noreply.github.com>
@asturur
Copy link
Member Author

asturur commented May 15, 2023

action items:

  • average function
  • Revert and test the negative hsl < -360
  • see if i can fit the template in the rgba regex ( hsla not worth ) i ll stick to docs
  • verify output is always trimmed correctly if alpha isn't rounded during parsing

ShaMan123
ShaMan123 previously approved these changes May 16, 2023
Comment on lines 83 to 92
* Calculate the grey average value for rgb and pass through alpha
*/
export const greyAverage = (
r: number,
g: number,
b: number,
a = 1
): [average: number, alpha: number] => [
Math.round(r * 0.3 + g * 0.59 + b * 0.11),
a,
Copy link
Contributor

Choose a reason for hiding this comment

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

seems alpha doesn't belong here
but I understand you want it the usage to be simple to use (less lines of code)
I suggest then returning a source value.
[greyAvg, greyAvg, greyAvg, alpha]

It is a bit weird like this, but not a big deal.

Copy link
Member Author

Choose a reason for hiding this comment

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

yes maybe we can return the source format, is an internal util, it can do whatever it needs to do to make the code efficient

Copy link
Member Author

Choose a reason for hiding this comment

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

we can make it from source to source.
Easier.

@asturur
Copy link
Member Author

asturur commented May 18, 2023

ok toHex and toHexa had a decimal bug, but now is forced as rounded

@asturur
Copy link
Member Author

asturur commented May 18, 2023

I think i addressed all comments

@asturur asturur merged commit 23999dc into master May 18, 2023
18 checks passed
@asturur asturur deleted the regex-color-cleanup branch May 18, 2023 22:51
@asturur asturur mentioned this pull request May 19, 2023
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.

None yet

2 participants