Skip to content

Commit e62a40d

Browse files
Implement reshape.trim (#302)
1 parent 9022bbf commit e62a40d

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

__TESTS__/unit/actions/Reshape.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,12 @@ describe('Tests for Transformation Action -- Cutter', () => {
7272

7373
expect(url).toBe('e_distort:1:2:3:4:5:6:7:8');
7474
});
75+
76+
it('Trims an image', () => {
77+
const url = createNewImage()
78+
.reshape(Reshape.trim().colorOverride('blue').colorSimilarity(15))
79+
.toString();
80+
81+
expect(url).toBe('e_trim:15:blue');
82+
});
7583
});

src/actions/reshape.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {ImageSource} from "../values/source/sourceTypes/ImageSource";
33
import {DistortArcAction} from "./reshape/DistortArc";
44
import {ShearAction} from "./reshape/Shear";
55
import {DistortAction, IDistortCoordinates} from "./reshape/Distort";
6+
import {TrimAction} from "./reshape/TrimAction";
67

78
type IReshape = CutByImage | DistortArcAction;
89

@@ -59,5 +60,15 @@ function shear(): ShearAction {
5960
return new ShearAction();
6061
}
6162

62-
const Reshape = {cutByImage, distortArc, distort, shear};
63-
export {cutByImage, Reshape, IReshape, distortArc, distort, shear};
63+
/**
64+
* @description Removes the edges of the image based on the color of the corner pixels.
65+
* Specify a color other than the color of the corner pixels using the colorOverride() method
66+
* @memberOf Actions.Reshape
67+
* @return {Actions.Reshape.TrimAction}
68+
*/
69+
function trim(): TrimAction {
70+
return new TrimAction();
71+
}
72+
73+
const Reshape = {cutByImage, distortArc, distort, shear, trim};
74+
export {cutByImage, Reshape, IReshape, distortArc, distort, shear, trim};

src/actions/reshape/TrimAction.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {Action} from "../../internal/Action";
2+
import {SystemColors} from "../../values/color";
3+
4+
/**
5+
* @description Removes the edges of the image based on the color of the corner pixels.
6+
* Specify a color other than the color of the corner pixels using the colorOverride() method
7+
* @memberOf Actions.Reshape
8+
* @extends {SDK.Action}
9+
*/
10+
class TrimAction extends Action {
11+
private _tolerance: number;
12+
private _color: SystemColors | string;
13+
14+
/**
15+
* @param {number} tolerance The tolerance level for color similarity.
16+
*/
17+
colorSimilarity(tolerance: number): this {
18+
this._tolerance = tolerance;
19+
return this;
20+
}
21+
22+
/**
23+
* @param {string | Values.Color} color Overrides the corner pixels color with the specified color.
24+
*/
25+
colorOverride(color: SystemColors | string): this {
26+
this._color = color;
27+
return this;
28+
}
29+
30+
toString(): string {
31+
// image.reshape(Reshape.trim()->colorSimilarity(50)->colorOverride(Color.YELLOW));
32+
// e_trim:50:yellow
33+
34+
return [
35+
'e_trim',
36+
this._tolerance,
37+
this._color
38+
].filter((a) => a).join(':');
39+
}
40+
}
41+
42+
43+
export {TrimAction};

0 commit comments

Comments
 (0)