Skip to content

Commit 318c35a

Browse files
committed
add makeTransparent fromJson
1 parent 9388edf commit 318c35a

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

__TESTS__/unit/fromJson/effect.fromJson.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ describe('effect.fromJson', () => {
1818
{ actionType: 'outline', width: 100, color: 'lightblue' },
1919
{ actionType: 'blackwhite', level: 40 },
2020
{ actionType: 'accelerate' },
21-
{ actionType: 'loop', iterations: 5 }
21+
{ actionType: 'loop', iterations: 5 },
22+
{ actionType: 'makeTransparent', tolerance: 5, color: 'red' }
2223

2324
]);
2425

@@ -38,7 +39,8 @@ describe('effect.fromJson', () => {
3839
'co_lightblue,e_outline:100',
3940
'e_blackwhite:40',
4041
'e_accelerate',
41-
'e_loop:5'
42+
'e_loop:5',
43+
'co_red,e_makeTransparent:5'
4244
].join('/'));
4345
});
4446
});

__TESTS__/unit/toJson/effect.toJson.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ describe('Effect toJson()', () => {
170170
{
171171
actionType: 'makeTransparent',
172172
tolerance: 5,
173-
level: 5,
174173
color: 'red'
175174
}
176175
]);

src/actions/effect/leveled/MakeTransparent.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {QualifierValue} from "../../../internal/qualifier/QualifierValue.js";
44
import {prepareColor} from "../../../internal/utils/prepareColor.js";
55
import {SystemColors} from "../../../qualifiers/color.js";
66
import {IMakeTransparentEffectModel} from "../../../internal/models/IEffectActionModel.js";
7+
import {IActionModel} from "../../../internal/models/IActionModel.js";
78

89
/**
910
* @description Makes the background of the image transparent (or solid white for formats that do not support transparency).
@@ -19,7 +20,9 @@ class MakeTransparentEffectAction extends LeveledEffectAction {
1920
*/
2021
tolerance(value: number | string): this {
2122
this._actionModel.tolerance = value as number;
22-
return this.setLevel(value);
23+
const qualifierEffect = this.createEffectQualifier(this.effectType, value);
24+
this.addQualifier(qualifierEffect);
25+
return this;
2326
}
2427

2528
/**
@@ -31,6 +34,18 @@ class MakeTransparentEffectAction extends LeveledEffectAction {
3134
this._actionModel.color = color;
3235
return this.addQualifier(new Qualifier('co', new QualifierValue(prepareColor(color))));
3336
}
37+
38+
static fromJson(actionModel: IActionModel): MakeTransparentEffectAction {
39+
const {actionType, tolerance, color } = (actionModel as IMakeTransparentEffectModel);
40+
41+
// We are using this() to allow inheriting classes to use super.fromJson.apply(this, [actionModel])
42+
// This allows the inheriting classes to determine the class to be created
43+
const result = new this(actionType, tolerance);
44+
tolerance && result.tolerance(tolerance);
45+
color && result.colorToReplace(color);
46+
47+
return result;
48+
}
3449
}
3550

3651

src/internal/fromJson.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {EffectOutline} from "../actions/effect/Outline.js";
2929
import {BlackwhiteEffectAction} from "../actions/effect/leveled/Blackwhite.js";
3030
import {AccelerationEffectAction} from "../actions/effect/leveled/Accelerate.js";
3131
import {LoopEffectAction} from "../actions/effect/leveled/Loop.js";
32+
import {MakeTransparentEffectAction} from "../actions/effect/leveled/MakeTransparent.js";
3233

3334
const ActionModelMap: Record<string, IHasFromJson> = {
3435
scale: ResizeScaleAction,
@@ -61,7 +62,8 @@ const ActionModelMap: Record<string, IHasFromJson> = {
6162
outline: EffectOutline,
6263
blackwhite: BlackwhiteEffectAction,
6364
accelerate: AccelerationEffectAction,
64-
loop: LoopEffectAction
65+
loop: LoopEffectAction,
66+
makeTransparent: MakeTransparentEffectAction
6567
};
6668

6769
/**

0 commit comments

Comments
 (0)