Skip to content

Commit e5196f0

Browse files
authored
Merge pull request #474 from cloudinary/feature/add-effect-toJson
Feature/add effect to json
2 parents 37d5135 + 3d117ae commit e5196f0

File tree

10 files changed

+174
-3
lines changed

10 files changed

+174
-3
lines changed

__TESTS_BUNDLE_SIZE__/bundleSizeTestCases.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ const bundleSizeTestCases:ITestCase[] = [
8888
},
8989
{
9090
name: 'Import All Actions',
91-
sizeLimitInKB: 32,
91+
sizeLimitInKB: 33,
9292
importsArray: [
9393
importFromPackage('Actions')
9494
]

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

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import {Transformation} from "../../../src";
22
import {Effect} from "../../../src/actions/effect";
33
import {halftone4x4Orthogonal} from "../../../src/qualifiers/dither";
4+
import {rodMonochromacy} from "../../../src/qualifiers/simulateColorBlind";
5+
import {Region} from "../../../src/qualifiers";
46

57

68
describe('Effect toJson()', () => {
@@ -206,4 +208,88 @@ describe('Effect toJson()', () => {
206208
}
207209
]);
208210
});
211+
212+
it('effect.vectorize', () => {
213+
const transformation = new Transformation()
214+
.addAction(Effect.vectorize().numOfColors(17).detailsLevel(100));
215+
expect(transformation.toJson()).toStrictEqual( [
216+
{
217+
actionType: 'vectorize',
218+
numOfColors: 17,
219+
detailLevel: 100
220+
}
221+
]);
222+
});
223+
224+
it('effect.gradientFade', () => {
225+
const transformation = new Transformation()
226+
.addAction(Effect.gradientFade()
227+
.strength(5)
228+
.horizontalStartPoint(10)
229+
.verticalStartPoint(20));
230+
expect(transformation.toJson()).toStrictEqual( [
231+
{
232+
actionType: 'GradientFade',
233+
strength: 5,
234+
horizontalStartPoint: 10,
235+
verticalStartPoint: 20
236+
}
237+
]);
238+
});
239+
240+
it('effect.assistColorBlind', () => {
241+
const transformation = new Transformation()
242+
.addAction(Effect.assistColorBlind().stripesStrength(20));
243+
expect(transformation.toJson()).toStrictEqual( [
244+
{
245+
actionType: 'assistColorblind',
246+
type: 'stripes',
247+
stripesStrength: 20
248+
}
249+
]);
250+
});
251+
252+
it('effect.simulateColorBlind', () => {
253+
const transformation = new Transformation()
254+
.addAction(Effect.simulateColorBlind().condition(rodMonochromacy()));
255+
expect(transformation.toJson()).toStrictEqual( [
256+
{ actionType: 'simulateColorblind',
257+
condition: 'rod_monochromacy'
258+
}
259+
]);
260+
});
261+
262+
it('effect.deshake', () => {
263+
const transformation = new Transformation()
264+
.addAction(Effect.deshake().shakeStrength(16));
265+
expect(transformation.toJson()).toStrictEqual( [
266+
{
267+
actionType: 'deshake',
268+
pixels: 16
269+
}
270+
]);
271+
});
272+
273+
it('effect.pixelate', () => {
274+
const transformation = new Transformation()
275+
.addAction(Effect.pixelate().squareSize(15).region(Region.faces()));
276+
expect(transformation.toJson()).toStrictEqual( [
277+
{
278+
actionType: 'pixelate',
279+
squareSize: 15,
280+
region: { RegionType: 'faces' }
281+
}
282+
]);
283+
});
284+
285+
it('effect.blur', () => {
286+
const transformation = new Transformation()
287+
.addAction(Effect.blur().strength(5));
288+
expect(transformation.toJson()).toStrictEqual( [
289+
{
290+
actionType: 'blur',
291+
strength: 5,
292+
}
293+
]);
294+
});
209295
});

src/actions/effect/AssistColorBlind.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {Action} from "../../internal/Action.js";
22
import {QualifierValue} from "../../internal/qualifier/QualifierValue.js";
33
import {Qualifier} from "../../internal/qualifier/Qualifier.js";
4+
import {IAssistColorBlindEffectModel} from "../../internal/models/IEffectActionModel.js";
45

56
/**
67
* @description Applies stripes to the image to help people with common color-blind conditions to differentiate between colors that are similar for them.
@@ -10,8 +11,10 @@ import {Qualifier} from "../../internal/qualifier/Qualifier.js";
1011
* @see Visit {@link Actions.Effect|Effect} for an example
1112
*/
1213
class AssistColorBlindEffectAction extends Action {
14+
protected _actionModel: IAssistColorBlindEffectModel = {};
1315
constructor() {
1416
super();
17+
this._actionModel.actionType = 'assistColorblind';
1518
this.addQualifier(new Qualifier('e', new QualifierValue('assist_colorblind')));
1619
}
1720

@@ -20,6 +23,7 @@ class AssistColorBlindEffectAction extends Action {
2023
* @return {this}
2124
*/
2225
xray(): this{
26+
this._actionModel.type = 'xray';
2327
return this.addQualifier(new Qualifier('e', new QualifierValue(['assist_colorblind', 'xray']).setDelimiter(':')));
2428
}
2529

@@ -29,6 +33,8 @@ class AssistColorBlindEffectAction extends Action {
2933
* @return {this}
3034
*/
3135
stripesStrength(strength:number | string): this {
36+
this._actionModel.type = 'stripes';
37+
this._actionModel.stripesStrength = strength;
3238
return this.addQualifier(new Qualifier('e', new QualifierValue(['assist_colorblind', strength]).setDelimiter(':')));
3339
}
3440
}

src/actions/effect/GradientFade.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {Action} from "../../internal/Action.js";
22
import {Qualifier} from "../../internal/qualifier/Qualifier.js";
3+
import {IGradientFadeEffecModel} from "../../internal/models/IEffectActionModel.js";
34

45
/**
56
* @description Applies a gradient fade effect from one edge of the image.
@@ -10,12 +11,14 @@ import {Qualifier} from "../../internal/qualifier/Qualifier.js";
1011
class GradientFadeEffectAction extends Action {
1112
private _strength: number;
1213
private _type: string;
14+
protected _actionModel: IGradientFadeEffecModel = {actionType: 'GradientFade'};
1315

1416
/**
1517
* @description Sets the strength of the fade effect.
1618
* @param {number} strength The strength of the fade effect. (Range: 0 to 100, Server default: 20)
1719
*/
1820
strength(strength:number): this {
21+
this._actionModel.strength = strength;
1922
this._strength = strength;
2023
return this;
2124
}
@@ -25,6 +28,7 @@ class GradientFadeEffectAction extends Action {
2528
* @param {string | Qualifiers.GradientFade} type The mode of gradient fade.
2629
*/
2730
type(type:string): this {
31+
this._actionModel.type = type;
2832
this._type = type;
2933
return this;
3034
}
@@ -34,6 +38,7 @@ class GradientFadeEffectAction extends Action {
3438
* @param {number | string} x The x dimension of the start point.
3539
*/
3640
horizontalStartPoint(x:number | string): this {
41+
this._actionModel.horizontalStartPoint = x as string;
3742
return this.addQualifier(new Qualifier('x', x));
3843
}
3944

@@ -42,6 +47,7 @@ class GradientFadeEffectAction extends Action {
4247
* @param {number | string} y The y dimension of the start point.
4348
*/
4449
verticalStartPoint(y:number | string): this {
50+
this._actionModel.verticalStartPoint = y as string;
4551
return this.addQualifier(new Qualifier('y', y));
4652
}
4753

src/actions/effect/SimulateColorBlind.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {Action} from "../../internal/Action.js";
22
import {Qualifier} from "../../internal/qualifier/Qualifier.js";
33
import {SimulateColorBlindType} from "../../types/types.js";
4+
import {ISimulateColorBlindEffectModel} from "../../internal/models/IEffectActionModel.js";
45

56
/**
67
* @description Simulates the way an image would appear to someone with the specified color blind condition
@@ -9,8 +10,10 @@ import {SimulateColorBlindType} from "../../types/types.js";
910
* @see Visit {@link Actions.Effect|Effect} for an example
1011
*/
1112
class SimulateColorBlindEffectAction extends Action {
13+
protected _actionModel: ISimulateColorBlindEffectModel = {};
1214
constructor() {
1315
super();
16+
this._actionModel.actionType = 'simulateColorblind';
1417
this.addQualifier(new Qualifier('e', `simulate_colorblind`));
1518
}
1619

@@ -28,6 +31,7 @@ class SimulateColorBlindEffectAction extends Action {
2831
* @return {this}
2932
*/
3033
condition(cond: SimulateColorBlindType | string): this {
34+
this._actionModel.condition = cond;
3135
return this.setQualifier(cond);
3236
}
3337
}

src/actions/effect/Vectorize.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {Action} from "../../internal/Action.js";
22
import {Qualifier} from "../../internal/qualifier/Qualifier.js";
33
import {QualifierValue} from "../../internal/qualifier/QualifierValue.js";
4+
import {IVectorizeEffectModel} from "../../internal/models/IEffectActionModel.js";
45

56
/**
67
* @description Vectorizes the image.
@@ -14,8 +15,10 @@ class VectorizeEffectAction extends Action {
1415
private _despeckleLevel: number | string;
1516
private _cornersLevel: number | string;
1617
private _paths: number | string;
18+
protected _actionModel: IVectorizeEffectModel = {};
1719
constructor() {
1820
super();
21+
this._actionModel.actionType = 'vectorize';
1922
}
2023

2124
/**
@@ -24,6 +27,7 @@ class VectorizeEffectAction extends Action {
2427
* @return {this}
2528
*/
2629
numOfColors(num: number | string): this {
30+
this._actionModel.numOfColors = num as number;
2731
this._numOfColors = num;
2832
return this;
2933
}
@@ -34,6 +38,7 @@ class VectorizeEffectAction extends Action {
3438
* @return {this}
3539
*/
3640
detailsLevel(num: number | string): this {
41+
this._actionModel.detailLevel = num as number;
3742
this._detailsLevel = num;
3843
return this;
3944
}
@@ -44,6 +49,7 @@ class VectorizeEffectAction extends Action {
4449
* @return {this}
4550
*/
4651
despeckleLevel(num: number | string):this {
52+
this._actionModel.despeckleLevel = num as number;
4753
this._despeckleLevel = num;
4854
return this;
4955
}
@@ -54,6 +60,7 @@ class VectorizeEffectAction extends Action {
5460
* @return {this}
5561
*/
5662
cornersLevel(num: number | string): this {
63+
this._actionModel.cornersLevel = num as number;
5764
this._cornersLevel = num;
5865
return this;
5966
}
@@ -64,6 +71,7 @@ class VectorizeEffectAction extends Action {
6471
* @return {this}
6572
*/
6673
paths(num: number):this {
74+
this._actionModel.paths = num;
6775
this._paths = num;
6876
return this;
6977
}

src/actions/effect/blur/Blur.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {NamedRegion} from "../../../qualifiers/region/NamedRegion.js";
22
import {Qualifier} from "../../../internal/qualifier/Qualifier.js";
33
import {Action} from "../../../internal/Action.js";
4+
import {IBlurModel} from "../../../internal/models/IEffectActionModel.js";
45

56
/**
67
* @description The Action class of the blur Builder.
@@ -11,17 +12,21 @@ import {Action} from "../../../internal/Action.js";
1112
class BlurAction extends Action {
1213
private _region?: NamedRegion;
1314
private _strength: number | string;
15+
protected _actionModel: IBlurModel = {};
1416

1517
constructor(strength: number | string) {
1618
super();
1719
this._strength = strength;
20+
this._actionModel.actionType = 'blur';
21+
this._actionModel.strength = strength as number;
1822
}
1923

2024
/**
2125
* @description Specifies the region to blur.
2226
* @param {NamedRegion} blurRegion
2327
*/
2428
region(blurRegion: NamedRegion): this {
29+
this._actionModel.region = {RegionType: blurRegion.regionType};
2530
this._region = blurRegion;
2631
return this;
2732
}
@@ -32,6 +37,7 @@ class BlurAction extends Action {
3237
*/
3338
strength(strength: number | string): this {
3439
this._strength = strength;
40+
this._actionModel.strength = strength as number;
3541
return this;
3642
}
3743

src/actions/effect/leveled/Deshake.ts

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

45
/**
56
* @description Removes small motion shifts from the video. with a maximum extent of movement in the horizontal and vertical direction of 32 pixels
@@ -8,12 +9,16 @@ import {ExpressionQualifier} from "../../../qualifiers/expression/ExpressionQual
89
* @see Visit {@link Actions.Effect|Effect} for an example
910
*/
1011
class DeshakeEffectAction extends LeveledEffectAction {
12+
protected _actionModel: IDeshakeEffectModel = {actionType: 'deshake'};
1113
/**
1214
* The maximum number of pixels in the horizontal and vertical direction that will be addressed. (Possible values: 16, 32, 48, 64. Server default: 16)
1315
* @param value Possible values: 16, 32, 48, 64. Server default: 16.
1416
*/
1517
shakeStrength(value: 16 | 32 | 48 | 64 | string | ExpressionQualifier): this {
16-
return this.setLevel(value);
18+
this._actionModel.pixels = value as number;
19+
const qualifierEffect = this.createEffectQualifier(this.effectType, value);
20+
this.addQualifier(qualifierEffect);
21+
return this;
1722
}
1823
}
1924

src/actions/effect/pixelate/Pixelate.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {NamedRegion} from "../../../qualifiers/region/NamedRegion.js";
22
import {Qualifier} from "../../../internal/qualifier/Qualifier.js";
33
import {Action} from "../../../internal/Action.js";
4+
import {IPixelateModel} from "../../../internal/models/IEffectActionModel.js";
45

56
/**
67
* @description The Action class of the pixelate Builder
@@ -11,10 +12,13 @@ import {Action} from "../../../internal/Action.js";
1112
class Pixelate extends Action {
1213
private _region?: NamedRegion;
1314
private _squareSize: number | string;
15+
protected _actionModel: IPixelateModel = {};
1416

1517
constructor(squareSize: number | string) {
1618
super();
1719
this._squareSize = squareSize;
20+
this._actionModel.actionType = 'pixelate';
21+
this._actionModel.squareSize = squareSize as number;
1822
}
1923

2024
/**
@@ -23,6 +27,7 @@ class Pixelate extends Action {
2327
*/
2428
region(pixelateRegion: NamedRegion): this {
2529
this._region = pixelateRegion;
30+
this._actionModel.region = {RegionType: this._region.regionType};
2631
return this;
2732
}
2833

@@ -32,6 +37,7 @@ class Pixelate extends Action {
3237
*/
3338
squareSize(squareSize: number | string): this {
3439
this._squareSize = squareSize;
40+
this._actionModel.squareSize = squareSize as number;
3541
return this;
3642
}
3743

0 commit comments

Comments
 (0)