Skip to content

Commit bf0aa87

Browse files
committed
add dither fromJson
1 parent 0ccd94a commit bf0aa87

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ describe('effect.fromJson', () => {
2121
{ actionType: 'loop', iterations: 5 },
2222
{ actionType: 'makeTransparent', tolerance: 5, color: 'red' },
2323
{ actionType: 'noise', level: 50 },
24-
{ actionType: 'vignette', level: 5 }
24+
{ actionType: 'vignette', level: 5 },
25+
{ actionType: 'dither', type: 9 }
2526

2627
]);
2728

@@ -44,7 +45,8 @@ describe('effect.fromJson', () => {
4445
'e_loop:5',
4546
'co_red,e_makeTransparent:5',
4647
'e_noise:50',
47-
'e_vignette:5'
48+
'e_vignette:5',
49+
'e_dither:9'
4850
].join('/'));
4951
});
5052
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ describe('Effect toJson()', () => {
202202
.addAction(Effect.dither().type(halftone4x4Orthogonal()));
203203
expect(transformation.toJson()).toStrictEqual( [
204204
{
205-
actionType: 'Dither',
205+
actionType: 'dither',
206206
type: 9,
207207
}
208208
]);

src/actions/effect/Dither.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {LeveledEffectAction} from "./EffectActions/LeveledEffectAction.js";
22
import {IDitherModel} from "../../internal/models/IEffectActionModel.js";
3+
import {IActionModel} from "../../internal/models/IActionModel.js";
34

45
/**
56
* @description Applies an ordered dither filter to the image.
@@ -8,7 +9,7 @@ import {IDitherModel} from "../../internal/models/IEffectActionModel.js";
89
* @see Visit {@link Actions.Effect|Effect} for an example
910
*/
1011
class DitherEffectAction extends LeveledEffectAction {
11-
protected _actionModel: IDitherModel = {actionType: 'Dither'};
12+
protected _actionModel: IDitherModel = {actionType: 'dither'};
1213
/**
1314
*
1415
* @param {Qualifiers.Dither} ditherType - The dither type applied to the image
@@ -20,6 +21,17 @@ class DitherEffectAction extends LeveledEffectAction {
2021
this.addQualifier(qualifierEffect);
2122
return this;
2223
}
24+
25+
static fromJson(actionModel: IActionModel): DitherEffectAction {
26+
const {actionType, type} = (actionModel as IDitherModel);
27+
28+
// We are using this() to allow inheriting classes to use super.fromJson.apply(this, [actionModel])
29+
// This allows the inheriting classes to determine the class to be created
30+
const result = new this(actionType);
31+
type && result.type(type);
32+
33+
return result;
34+
}
2335
}
2436

2537

src/internal/fromJson.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ 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";
3232
import {MakeTransparentEffectAction} from "../actions/effect/leveled/MakeTransparent.js";
33+
import {DitherEffectAction} from "../actions/effect/Dither.js";
3334

3435
const ActionModelMap: Record<string, IHasFromJson> = {
3536
scale: ResizeScaleAction,
@@ -65,7 +66,9 @@ const ActionModelMap: Record<string, IHasFromJson> = {
6566
loop: LoopEffectAction,
6667
makeTransparent: MakeTransparentEffectAction,
6768
noise: EffectActionWithLevel,
68-
vignette: EffectActionWithStrength
69+
vignette: EffectActionWithStrength,
70+
dither: DitherEffectAction
71+
6972
};
7073

7174
/**

0 commit comments

Comments
 (0)