Skip to content

Commit 3f7b58e

Browse files
authored
Add colorize option (#44)
* Add option * Add changests
1 parent 3179557 commit 3f7b58e

6 files changed

Lines changed: 143 additions & 0 deletions

File tree

.changeset/fruity-beds-train.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@imgproxy/imgproxy-js-core": minor
3+
---
4+
5+
Add support for [colorize](https://docs.imgproxy.net/usage/processing#colorize) option

src/options/colorize.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import type { Colorize, ColorizeOptionsPartial } from "../types/colorize";
2+
import { guardIsNotNum, guardIsUndef } from "../utils";
3+
4+
const getOpt = (options: ColorizeOptionsPartial): Colorize | undefined =>
5+
options.colorize ?? options.col;
6+
7+
const test = (options: ColorizeOptionsPartial): boolean => {
8+
return Boolean(getOpt(options));
9+
};
10+
11+
const build = (options: ColorizeOptionsPartial): string => {
12+
const colorizeOpt = getOpt(options);
13+
14+
guardIsUndef(colorizeOpt, "colorize");
15+
16+
const { opacity, color, keepAlpha } = colorizeOpt;
17+
18+
guardIsNotNum(opacity, "colorize.opacity", {
19+
addParam: {
20+
min: 0,
21+
max: 1,
22+
},
23+
});
24+
25+
let result = `col:${opacity}`;
26+
27+
if (color) {
28+
result += `:${color}`;
29+
}
30+
31+
if (keepAlpha !== undefined) {
32+
// Add color parameter if it wasn't provided but keepAlpha is set
33+
if (!color) {
34+
result += ":";
35+
}
36+
result += `:${keepAlpha ? 1 : 0}`;
37+
}
38+
39+
return result;
40+
};
41+
42+
export { test, build };

src/options/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export * as blur from "./blur";
77
export * as blurDetections from "./blurDetections";
88
export * as brightness from "./brightness";
99
export * as cacheBuster from "../optionsShared/cacheBuster";
10+
export * as colorize from "./colorize";
1011
export * as contrast from "./contrast";
1112
export * as crop from "../optionsShared/crop";
1213
export * as disableAnimation from "./disableAnimation";

src/types/colorize.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* *Colorize option*. **PRO feature**
3+
*
4+
* Places a color overlay on the processed image.
5+
* @see
6+
* - `colorize` https://docs.imgproxy.net/generating_the_url?id=colorize
7+
*/
8+
export interface Colorize {
9+
/** Opacity of the color overlay (0-1). When set to 0, overlay is not applied */
10+
opacity: number;
11+
/** Optional hex-coded color value (default is black: 000) */
12+
color?: string;
13+
/** Optional flag to preserve the original image's alpha channel (default is false) */
14+
keepAlpha?: boolean;
15+
}
16+
17+
export interface ColorizeOptionsPartial {
18+
colorize?: Colorize;
19+
col?: Colorize;
20+
}

src/types/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { BlurDetectionsOptionsPartial } from "./blurDetections";
77
import type { BlurOptionsPartial } from "./blur";
88
import type { BrightnessOptionsPartial } from "./brightness";
99
import type { CacheBusterOptionsPartial } from "../typesShared/cacheBuster";
10+
import type { ColorizeOptionsPartial } from "./colorize";
1011
import type { ContrastOptionsPartial } from "./contrast";
1112
import type { CropOptionsPartial } from "../typesShared/crop";
1213
import type { DisableAnimationOptionsPartial } from "./disableAnimation";
@@ -81,6 +82,7 @@ export type Options = AdjustOptionsPartial &
8182
BlurOptionsPartial &
8283
BrightnessOptionsPartial &
8384
CacheBusterOptionsPartial &
85+
ColorizeOptionsPartial &
8486
ContrastOptionsPartial &
8587
CropOptionsPartial &
8688
DisableAnimationOptionsPartial &
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { it, expect, describe, assertType } from "vitest";
2+
import { test, build } from "../../src/options/colorize";
3+
import { Options } from "../../src/types";
4+
5+
describe("colorize", () => {
6+
describe("test", () => {
7+
it("should test true if colorize is set", () => {
8+
expect(test({ colorize: { opacity: 0.5 } })).toBe(true);
9+
});
10+
11+
it("should test true if col is set", () => {
12+
expect(test({ col: { opacity: 0.5 } })).toBe(true);
13+
});
14+
15+
it("should test false if colorize is not set", () => {
16+
expect(test({})).toBe(false);
17+
});
18+
});
19+
20+
describe("build", () => {
21+
it("should work with opacity only", () => {
22+
expect(build({ colorize: { opacity: 0 } })).toBe("col:0");
23+
expect(build({ colorize: { opacity: 1 } })).toBe("col:1");
24+
expect(build({ colorize: { opacity: 0.4 } })).toBe("col:0.4");
25+
});
26+
27+
it("should work with opacity and color", () => {
28+
expect(build({ colorize: { opacity: 0.5, color: "ff0000" } })).toBe(
29+
"col:0.5:ff0000"
30+
);
31+
});
32+
33+
it("should work with opacity, color, and keepAlpha", () => {
34+
expect(
35+
build({ colorize: { opacity: 0.5, color: "ff0000", keepAlpha: true } })
36+
).toBe("col:0.5:ff0000:1");
37+
expect(
38+
build({ colorize: { opacity: 0.5, color: "ff0000", keepAlpha: false } })
39+
).toBe("col:0.5:ff0000:0");
40+
});
41+
42+
it("should work with opacity and keepAlpha without color", () => {
43+
expect(build({ colorize: { opacity: 0.5, keepAlpha: true } })).toBe(
44+
"col:0.5::1"
45+
);
46+
expect(build({ colorize: { opacity: 0.5, keepAlpha: false } })).toBe(
47+
"col:0.5::0"
48+
);
49+
});
50+
51+
it("should validate opacity", () => {
52+
expect(() => build({ colorize: { opacity: -1 } })).toThrow();
53+
expect(() => build({ colorize: { opacity: 2 } })).toThrow();
54+
});
55+
});
56+
57+
describe("Check types", () => {
58+
it("check TS type declaration", () => {
59+
assertType<Options>({
60+
colorize: {
61+
opacity: 0.5,
62+
color: "ff0000",
63+
keepAlpha: true,
64+
},
65+
col: {
66+
opacity: 0.5,
67+
color: "ff0000",
68+
keepAlpha: false,
69+
},
70+
});
71+
});
72+
});
73+
});

0 commit comments

Comments
 (0)