Skip to content

Commit

Permalink
chore(): Reduce some verbose code (#10311)
Browse files Browse the repository at this point in the history
  • Loading branch information
asturur authored Dec 4, 2024
1 parent 1f62cde commit 809ed81
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 59 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [next]

- chore(): Reduce some verbose code [#10311](https://github.com/fabricjs/fabric.js/pull/10311)
- ci(): Test node 22 again [#10310](https://github.com/fabricjs/fabric.js/pull/10310)
- chore(TS): Try to remove all TS-ERROR directives [#10309](https://github.com/fabricjs/fabric.js/pull/10309)
- chore(): Make TS config more strict [#10308](https://github.com/fabricjs/fabric.js/pull/10308)
Expand Down
4 changes: 1 addition & 3 deletions src/canvas/Canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1099,9 +1099,7 @@ export class Canvas extends SelectableCanvas implements CanvasOptions {
* @private
*/
_resetTransformEventData() {
this._target = undefined;
this._pointer = undefined;
this._absolutePointer = undefined;
this._target = this._pointer = this._absolutePointer = undefined;
}

/**
Expand Down
74 changes: 39 additions & 35 deletions src/filters/BlendColor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,76 +101,80 @@ export class BlendColor extends BaseFilter<'BlendColor', BlendColorOwnProps> {
*/
applyTo2d({ imageData: { data } }: T2DPipelineState) {
const source = new Color(this.color).getSource();
const tr = source[0] * this.alpha;
const tg = source[1] * this.alpha;
const tb = source[2] * this.alpha;
const alpha1 = 1 - this.alpha;
const alpha = this.alpha;
const tr = source[0] * alpha;
const tg = source[1] * alpha;
const tb = source[2] * alpha;
const alpha1 = 1 - alpha;

for (let i = 0; i < data.length; i += 4) {
const r = data[i];
const g = data[i + 1];
const b = data[i + 2];

let oR, oG, oB;
switch (this.mode) {
case 'multiply':
data[i] = (r * tr) / 255;
data[i + 1] = (g * tg) / 255;
data[i + 2] = (b * tb) / 255;
oR = (r * tr) / 255;
oG = (g * tg) / 255;
oB = (b * tb) / 255;
break;
case 'screen':
data[i] = 255 - ((255 - r) * (255 - tr)) / 255;
data[i + 1] = 255 - ((255 - g) * (255 - tg)) / 255;
data[i + 2] = 255 - ((255 - b) * (255 - tb)) / 255;
oR = 255 - ((255 - r) * (255 - tr)) / 255;
oG = 255 - ((255 - g) * (255 - tg)) / 255;
oB = 255 - ((255 - b) * (255 - tb)) / 255;
break;
case 'add':
data[i] = r + tr;
data[i + 1] = g + tg;
data[i + 2] = b + tb;
oR = r + tr;
oG = g + tg;
oB = b + tb;
break;
case 'difference':
data[i] = Math.abs(r - tr);
data[i + 1] = Math.abs(g - tg);
data[i + 2] = Math.abs(b - tb);
oR = Math.abs(r - tr);
oG = Math.abs(g - tg);
oB = Math.abs(b - tb);
break;
case 'subtract':
data[i] = r - tr;
data[i + 1] = g - tg;
data[i + 2] = b - tb;
oR = r - tr;
oG = g - tg;
oB = b - tb;
break;
case 'darken':
data[i] = Math.min(r, tr);
data[i + 1] = Math.min(g, tg);
data[i + 2] = Math.min(b, tb);
oR = Math.min(r, tr);
oG = Math.min(g, tg);
oB = Math.min(b, tb);
break;
case 'lighten':
data[i] = Math.max(r, tr);
data[i + 1] = Math.max(g, tg);
data[i + 2] = Math.max(b, tb);
oR = Math.max(r, tr);
oG = Math.max(g, tg);
oB = Math.max(b, tb);
break;
case 'overlay':
data[i] =
oR =
tr < 128
? (2 * r * tr) / 255
: 255 - (2 * (255 - r) * (255 - tr)) / 255;
data[i + 1] =
oG =
tg < 128
? (2 * g * tg) / 255
: 255 - (2 * (255 - g) * (255 - tg)) / 255;
data[i + 2] =
oB =
tb < 128
? (2 * b * tb) / 255
: 255 - (2 * (255 - b) * (255 - tb)) / 255;
break;
case 'exclusion':
data[i] = tr + r - (2 * tr * r) / 255;
data[i + 1] = tg + g - (2 * tg * g) / 255;
data[i + 2] = tb + b - (2 * tb * b) / 255;
oR = tr + r - (2 * tr * r) / 255;
oG = tg + g - (2 * tg * g) / 255;
oB = tb + b - (2 * tb * b) / 255;
break;
case 'tint':
data[i] = tr + r * alpha1;
data[i + 1] = tg + g * alpha1;
data[i + 2] = tb + b * alpha1;
oR = tr + r * alpha1;
oG = tg + g * alpha1;
oB = tb + b * alpha1;
}
data[i] = oR;
data[i + 1] = oG;
data[i + 2] = oB;
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/filters/Brightness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ export class Brightness extends BaseFilter<'Brightness', BrightnessOwnProps> {
applyTo2d({ imageData: { data } }: T2DPipelineState) {
const brightness = Math.round(this.brightness * 255);
for (let i = 0; i < data.length; i += 4) {
data[i] = data[i] + brightness;
data[i + 1] = data[i + 1] + brightness;
data[i + 2] = data[i + 2] + brightness;
data[i] += brightness;
data[i + 1] += brightness;
data[i + 2] += brightness;
}
}

Expand Down
16 changes: 7 additions & 9 deletions src/filters/Grayscale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,22 @@ export class Grayscale extends BaseFilter<'Grayscale', GrayscaleOwnProps> {
*/
applyTo2d({ imageData: { data } }: T2DPipelineState) {
for (let i = 0, value: number; i < data.length; i += 4) {
const r = data[i];
const g = data[i + 1];
const b = data[i + 2];
switch (this.mode) {
case 'average':
value = (data[i] + data[i + 1] + data[i + 2]) / 3;
value = (r + g + b) / 3;
break;
case 'lightness':
value =
(Math.min(data[i], data[i + 1], data[i + 2]) +
Math.max(data[i], data[i + 1], data[i + 2])) /
2;
value = (Math.min(r, g, b) + Math.max(r, g, b)) / 2;
break;
case 'luminosity':
value = 0.21 * data[i] + 0.72 * data[i + 1] + 0.07 * data[i + 2];
value = 0.21 * r + 0.72 * g + 0.07 * b;
break;
}

data[i] = value;
data[i + 1] = value;
data[i + 2] = value;
data[i + 2] = data[i + 1] = data[i] = value;
}
}

Expand Down
11 changes: 7 additions & 4 deletions src/filters/Saturation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,13 @@ export class Saturation extends BaseFilter<'Saturation', SaturationOwnProps> {
applyTo2d({ imageData: { data } }: T2DPipelineState) {
const adjust = -this.saturation;
for (let i = 0; i < data.length; i += 4) {
const max = Math.max(data[i], data[i + 1], data[i + 2]);
data[i] += max !== data[i] ? (max - data[i]) * adjust : 0;
data[i + 1] += max !== data[i + 1] ? (max - data[i + 1]) * adjust : 0;
data[i + 2] += max !== data[i + 2] ? (max - data[i + 2]) * adjust : 0;
const r = data[i];
const g = data[i + 1];
const b = data[i + 2];
const max = Math.max(r, g, b);
data[i] += max !== r ? (max - r) * adjust : 0;
data[i + 1] += max !== g ? (max - g) * adjust : 0;
data[i + 2] += max !== b ? (max - b) * adjust : 0;
}
}

Expand Down
13 changes: 8 additions & 5 deletions src/filters/Vibrance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,15 @@ export class Vibrance extends BaseFilter<'Vibrance', VibranceOwnProps> {
applyTo2d({ imageData: { data } }: T2DPipelineState) {
const adjust = -this.vibrance;
for (let i = 0; i < data.length; i += 4) {
const max = Math.max(data[i], data[i + 1], data[i + 2]);
const avg = (data[i] + data[i + 1] + data[i + 2]) / 3;
const r = data[i];
const g = data[i + 1];
const b = data[i + 2];
const max = Math.max(r, g, b);
const avg = (r + g + b) / 3;
const amt = ((Math.abs(max - avg) * 2) / 255) * adjust;
data[i] += max !== data[i] ? (max - data[i]) * amt : 0;
data[i + 1] += max !== data[i + 1] ? (max - data[i + 1]) * amt : 0;
data[i + 2] += max !== data[i + 2] ? (max - data[i + 2]) * amt : 0;
data[i] += max !== r ? (max - r) * amt : 0;
data[i + 1] += max !== g ? (max - g) * amt : 0;
data[i + 2] += max !== b ? (max - b) * amt : 0;
}
}

Expand Down

0 comments on commit 809ed81

Please sign in to comment.