Skip to content

Commit 3fd319e

Browse files
authored
Merge pull request #485 from cloudinary/feature/add-adjust-model-values
Feature/add adjust model values
2 parents af95846 + fb6b334 commit 3fd319e

File tree

6 files changed

+87
-2
lines changed

6 files changed

+87
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {fromJson} from "../../../src/internal/fromJson";
2+
3+
describe('adjust.fromJson', () => {
4+
it('should generate a url with adjust actions from array of models', function () {
5+
const transformation = fromJson([
6+
{ actionType: 'improve', mode: 'outdoor', blend: 30},
7+
{ actionType: 'unsharpMask', level: 50},
8+
{ actionType: 'saturation', level: 40}
9+
]);
10+
11+
expect(transformation.toString()).toStrictEqual([
12+
'e_improve:outdoor:30',
13+
'e_unsharp_mask:50',
14+
'e_saturation:40'
15+
].join('/'));
16+
});
17+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import {Transformation} from "../../../src";
2+
import {Adjust} from "../../../src/actions/adjust";
3+
4+
describe('Adjust toJson()', () => {
5+
it('adjust.improve', () => {
6+
const transformation = new Transformation()
7+
.addAction(Adjust.improve().mode('outdoor').blend(50));
8+
expect(transformation.toJson()).toStrictEqual( [
9+
{
10+
actionType: 'improve',
11+
mode: 'outdoor',
12+
blend: 50
13+
}
14+
]);
15+
});
16+
17+
it('adjust.unsharpMask', () => {
18+
const transformation = new Transformation()
19+
.addAction(Adjust.unsharpMask().strength(10));
20+
expect(transformation.toJson()).toStrictEqual( [
21+
{
22+
actionType: 'unsharpMask',
23+
level: 10
24+
}
25+
]);
26+
});
27+
28+
it('adjust.saturation', () => {
29+
const transformation = new Transformation()
30+
.addAction(Adjust.saturation().level(50));
31+
expect(transformation.toJson()).toStrictEqual( [
32+
{
33+
actionType: 'saturation',
34+
level: 50
35+
}
36+
]);
37+
});
38+
});

src/actions/adjust/ImproveAction.ts

+17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import {Action} from "../../internal/Action.js";
22
import {QualifierValue} from "../../internal/qualifier/QualifierValue.js";
33
import {Qualifier} from "../../internal/qualifier/Qualifier.js";
44
import {stringOrNumber} from "../../types/types.js";
5+
import {ImproveActionModel} from "../../internal/models/IAdjustActionModel.js";
6+
import {IActionModel} from "../../internal/models/IActionModel.js";
57

68
/**
79
* @description Defines how to improve an image by automatically adjusting image colors, contrast and brightness.</br>
@@ -11,6 +13,7 @@ import {stringOrNumber} from "../../types/types.js";
1113
class ImproveAction extends Action {
1214
private modeValue:stringOrNumber;
1315
private blendValue:number;
16+
protected _actionModel: ImproveActionModel = {actionType: 'improve'};
1417
constructor() {
1518
super();
1619
}
@@ -22,6 +25,7 @@ class ImproveAction extends Action {
2225
*/
2326
mode(value: 'outdoor' | 'indoor' | string): this {
2427
this.modeValue = value;
28+
this._actionModel.mode = value;
2529
return this;
2630
}
2731

@@ -31,6 +35,7 @@ class ImproveAction extends Action {
3135
*/
3236
blend(value:number): this {
3337
this.blendValue = value;
38+
this._actionModel.blend = value;
3439
return this;
3540
}
3641

@@ -39,6 +44,18 @@ class ImproveAction extends Action {
3944
this.addQualifier(new Qualifier('e', qualifierValue));
4045
return this;
4146
}
47+
48+
static fromJson(actionModel: IActionModel): ImproveAction{
49+
const {mode, blend} = (actionModel as ImproveActionModel);
50+
51+
// We are using this() to allow inheriting classes to use super.fromJson.apply(this, [actionModel])
52+
// This allows the inheriting classes to determine the class to be created
53+
const result = new this();
54+
mode && result.mode(mode);
55+
blend && result.blend(blend);
56+
57+
return result;
58+
}
4259
}
4360

4461
export {ImproveAction};

src/internal/fromJson.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {SimulateColorBlindEffectAction} from "../actions/effect/SimulateColorBli
3838
import {DeshakeEffectAction} from "../actions/effect/leveled/Deshake.js";
3939
import {Pixelate} from "../actions/effect/pixelate/Pixelate.js";
4040
import {BlurAction} from "../actions/effect/blur/Blur.js";
41+
import {ImproveAction} from "../actions/adjust/ImproveAction.js";
4142

4243
const ActionModelMap: Record<string, IHasFromJson> = {
4344
scale: ResizeScaleAction,
@@ -81,7 +82,10 @@ const ActionModelMap: Record<string, IHasFromJson> = {
8182
simulateColorblind: SimulateColorBlindEffectAction,
8283
deshake: DeshakeEffectAction,
8384
pixelate: Pixelate,
84-
blur: BlurAction
85+
blur: BlurAction,
86+
improve: ImproveAction,
87+
unsharpMask: EffectActionWithStrength,
88+
saturation: EffectActionWithLevel
8589
};
8690

8791
/**

src/internal/internalConstants.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ export const ACTION_TYPE_TO_DELIVERY_MODE_MAP: Record<string, string> = {
104104
export const ACTION_TYPE_TO_EFFECT_MODE_MAP: Record<string, string> = {
105105
redEye: 'redeye',
106106
advancedRedEye: 'adv_redeye',
107-
oilPaint: 'oil_paint'
107+
oilPaint: 'oil_paint',
108+
unsharpMask: 'unsharp_mask'
108109
};
109110

110111
export const CHROMA_VALUE_TO_CHROMA_MODEL_ENUM: Record<number, string> = {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IActionModel} from "./IActionModel.js";
2+
3+
interface ImproveActionModel extends IActionModel {
4+
mode?: string;
5+
blend?: number;
6+
}
7+
8+
export {ImproveActionModel};

0 commit comments

Comments
 (0)