diff --git a/__TESTS__/unit/actions/Reshape.test.ts b/__TESTS__/unit/actions/Reshape.test.ts index 82e8c957..e23a1df3 100644 --- a/__TESTS__/unit/actions/Reshape.test.ts +++ b/__TESTS__/unit/actions/Reshape.test.ts @@ -72,4 +72,12 @@ describe('Tests for Transformation Action -- Cutter', () => { expect(url).toBe('e_distort:1:2:3:4:5:6:7:8'); }); + + it('Trims an image', () => { + const url = createNewImage() + .reshape(Reshape.trim().colorOverride('blue').colorSimilarity(15)) + .toString(); + + expect(url).toBe('e_trim:15:blue'); + }); }); diff --git a/src/actions/reshape.ts b/src/actions/reshape.ts index 550a0aa9..c7b8fd57 100644 --- a/src/actions/reshape.ts +++ b/src/actions/reshape.ts @@ -3,6 +3,7 @@ import {ImageSource} from "../values/source/sourceTypes/ImageSource"; import {DistortArcAction} from "./reshape/DistortArc"; import {ShearAction} from "./reshape/Shear"; import {DistortAction, IDistortCoordinates} from "./reshape/Distort"; +import {TrimAction} from "./reshape/TrimAction"; type IReshape = CutByImage | DistortArcAction; @@ -59,5 +60,15 @@ function shear(): ShearAction { return new ShearAction(); } -const Reshape = {cutByImage, distortArc, distort, shear}; -export {cutByImage, Reshape, IReshape, distortArc, distort, shear}; +/** + * @description Removes the edges of the image based on the color of the corner pixels. + * Specify a color other than the color of the corner pixels using the colorOverride() method + * @memberOf Actions.Reshape + * @return {Actions.Reshape.TrimAction} + */ +function trim(): TrimAction { + return new TrimAction(); +} + +const Reshape = {cutByImage, distortArc, distort, shear, trim}; +export {cutByImage, Reshape, IReshape, distortArc, distort, shear, trim}; diff --git a/src/actions/reshape/TrimAction.ts b/src/actions/reshape/TrimAction.ts new file mode 100644 index 00000000..940da825 --- /dev/null +++ b/src/actions/reshape/TrimAction.ts @@ -0,0 +1,43 @@ +import {Action} from "../../internal/Action"; +import {SystemColors} from "../../values/color"; + +/** + * @description Removes the edges of the image based on the color of the corner pixels. + * Specify a color other than the color of the corner pixels using the colorOverride() method + * @memberOf Actions.Reshape + * @extends {SDK.Action} + */ +class TrimAction extends Action { + private _tolerance: number; + private _color: SystemColors | string; + + /** + * @param {number} tolerance The tolerance level for color similarity. + */ + colorSimilarity(tolerance: number): this { + this._tolerance = tolerance; + return this; + } + + /** + * @param {string | Values.Color} color Overrides the corner pixels color with the specified color. + */ + colorOverride(color: SystemColors | string): this { + this._color = color; + return this; + } + + toString(): string { + // image.reshape(Reshape.trim()->colorSimilarity(50)->colorOverride(Color.YELLOW)); + // e_trim:50:yellow + + return [ + 'e_trim', + this._tolerance, + this._color + ].filter((a) => a).join(':'); + } +} + + +export {TrimAction};