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
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
c26f998
not sure is a cleanup
asturur May 13, 2023
9792c44
not sure is a cleanup
asturur May 13, 2023
8db4286
lots of comments
asturur May 13, 2023
048eb28
lots of comments and extended regexes
asturur May 13, 2023
453248f
removed extra console log
asturur May 13, 2023
a962ce1
color code cleanup
asturur May 13, 2023
b3bca21
color code cleanup
asturur May 13, 2023
3eb2bf7
even better
asturur May 13, 2023
b445f9c
added changelog
asturur May 13, 2023
f17c216
Merge branch 'master' into regex-color-cleanup
asturur May 13, 2023
979bfb3
Merge branch 'master' into regex-color-cleanup
asturur May 14, 2023
29a5598
in the process of changing tests, pause needed
asturur May 14, 2023
5a5c134
Merge branch 'regex-color-cleanup' of github.com:fabricjs/fabric.js i…
asturur May 14, 2023
4d15b1c
ok seen those
asturur May 14, 2023
89ccb5f
more test and fixed floats
asturur May 14, 2023
f3eadd8
hopefully better code
asturur May 14, 2023
ed671f0
hopefully better code
asturur May 14, 2023
77d2dbb
converted tests
asturur May 14, 2023
d4e98a5
moved method to util
asturur May 14, 2023
649ba3c
missed lint
asturur May 14, 2023
02f0c5a
Update src/color/Color.ts
asturur May 15, 2023
2406fa3
Update src/color/constants.ts
asturur May 15, 2023
e3bfa4c
Update src/color/constants.ts
asturur May 15, 2023
b648cb4
Update src/color/constants.ts
asturur May 15, 2023
c78c568
Update src/color/util.ts
asturur May 15, 2023
43a2d1c
Update src/color/constants.ts
asturur May 15, 2023
210a24a
save those
asturur May 16, 2023
fd75870
Merge branch 'regex-color-cleanup' of github.com:fabricjs/fabric.js i…
asturur May 16, 2023
4b3693f
test with negative and revert
asturur May 16, 2023
ed121bc
Merge branch 'master' into regex-color-cleanup
asturur May 18, 2023
d90d07a
grey average redone
asturur May 18, 2023
713a425
ensure hexify is rounder
asturur May 18, 2023
362879f
added test
asturur May 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/color/Color.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { ColorNameMap } from './color_map';
import { reHSLa, reHex, reRGBa } from './constants';
import type { TRGBAColorSource, TColorArg } from './typedefs';
import { hue2rgb, hexify, rgb2Hsl, fromAlphaToFloat } from './util';
import {
hue2rgb,
hexify,
rgb2Hsl,
fromAlphaToFloat,
greyAverage,
} from './util';

/**
* @class Color common color operations
Expand Down Expand Up @@ -139,8 +145,7 @@ export class Color {
* @return {Color} thisArg
*/
toGrayscale() {
const [r, g, b, a] = this.getSource(),
average = Math.round(r * 0.3 + g * 0.59 + b * 0.11);
const [average, a] = greyAverage(...this.getSource());
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

}
Expand All @@ -151,8 +156,7 @@ export class Color {
* @return {Color} thisArg
*/
toBlackWhite(threshold: number) {
const [r, g, b, a] = this.getSource(),
average = Math.round(r * 0.3 + g * 0.59 + b * 0.11),
const [average, a] = greyAverage(...this.getSource()),
bOrW = average < (threshold || 127) ? 0 : 255;
this.setSource([bOrW, bOrW, bOrW, a]);
return this;
Expand Down
10 changes: 5 additions & 5 deletions src/color/constants.ts
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?!

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Regex matching color in RGB or RGBA formats (ex: rgb(0, 0, 0), rgba(255, 100, 10, 0.5), rgba( 255 , 100 , 10 , 0.5 ), rgb(1,1,1), rgba(100%, 60%, 10%, 0.5))
* Regex matching color in RGB or RGBA formats (ex: `rgb(0, 0, 0)`, `rgba(255, 100, 10, 0.5)`, `rgba( 255 , 100 , 10 , 0.5 )`, `rgb(1,1,1)`, `rgba(100%, 60%, 10%, 0.5)`)
* Also matching rgba(r g b / a) as per new specs
* https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/rgb
* Formal syntax at the time of writing:
Expand Down Expand Up @@ -49,7 +49,7 @@
* The alpha channel can be in the format 0.4 .7 or 1 or 73%
*
* WARNING this regex doesn't fail on off spec colors. it matches everything that could be a color.
* So the spec does not allow for rgba(30 , 45% 35, 49%) but this will work anyway for us
* So the spec does not allow for `rgba(30 , 45% 35, 49%)` but this will work anyways for us
*/
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.

Expand Down Expand Up @@ -95,11 +95,11 @@ export const reRGBa = () =>
* \) // Matches the closing parenthesis
* $/i // Matches the end of the string and sets the regular expression to case-insensitive mode
*
* WARNING this regex doesn't fail on off spec colors. it matches everything that could be a color.
* So the spec does not allow for hsl(30 , 45% 35, 49%) but this will work anyway for us
* WARNING this regex doesn't fail on off spec colors. It matches everything that could be a color.
* So the spec does not allow `hsl(30 , 45% 35, 49%)` but this will work anyways for us.
*/
export const reHSLa = () =>
/^hsla?\(\s*(\d{1,3})\s*[\s|,]\s*(\d{1,3}%)\s*[\s|,]\s*(\d{1,3}%)\s*(?:\s*[,/]\s*(\d*(?:\.\d+)?%?)\s*)?\)$/i;
/^hsla?\(\s*([+-]?\d{1,3})\s*[\s|,]\s*(\d{1,3}%)\s*[\s|,]\s*(\d{1,3}%)\s*(?:\s*[,/]\s*(\d*(?:\.\d+)?%?)\s*)?\)$/i;

/**
* Regex matching color in HEX format (ex: #FF5544CC, #FF5555, 010155, aff)
Expand Down
15 changes: 14 additions & 1 deletion src/color/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,20 @@ export const fromAlphaToFloat = (value = '1') =>
parseFloat(value) / (value.endsWith('%') ? 100 : 1);

/**
* Convert a value [0, 255] to hex
* Convert a value in the inclusive range [0, 255] to hex
*/
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.


/**
* 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.

];
6 changes: 5 additions & 1 deletion test/unit/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,12 @@
expectedSource: [89, 191, 64, 0.2]
},{
name: 'fromHsla no commas(with whitespaces)',
stringToParse: 'hsl( 108 50% 50% / .5)',
stringToParse: 'hsl( 108 50% 50% / .5)',
expectedSource: [89, 191, 64, 0.5]
},{
name: 'fromHsla with very counterClockwise value)',
stringToParse: 'hsl( -450, 50%, 50%, .5)',
expectedSource: [127, 64, 191, 0.5]
}].forEach(({ name, stringToParse, expectedSource }) => {
QUnit.test(name, function(assert) {
var oColor = fabric.Color.fromHsla(stringToParse);
Expand Down