Skip to content

Commit 6f9bcd0

Browse files
authored
Add color_profile option support (#61)
* Add color_profile option * Add changesets
1 parent e487f60 commit 6f9bcd0

File tree

6 files changed

+123
-0
lines changed

6 files changed

+123
-0
lines changed

.changeset/deep-lines-sort.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 `color_profile` option support

src/options/colorProfile.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import type {
2+
ColorProfile,
3+
ColorProfileOptionsPartial,
4+
} from "../types/colorProfile";
5+
import { guardIsUndef } from "../utils";
6+
7+
const getOpt = (
8+
options: ColorProfileOptionsPartial
9+
): ColorProfile | undefined => {
10+
if ("color_profile" in options) {
11+
return options.color_profile;
12+
} else if ("cp" in options) {
13+
return options.cp;
14+
} else if ("icc" in options) {
15+
return options.icc;
16+
}
17+
18+
return undefined;
19+
};
20+
21+
const test = (options: ColorProfileOptionsPartial): boolean =>
22+
getOpt(options) !== undefined;
23+
24+
const build = (options: ColorProfileOptionsPartial): string => {
25+
const colorProfileOpts = getOpt(options);
26+
guardIsUndef(colorProfileOpts, "color_profile");
27+
return `cp:${colorProfileOpts}`;
28+
};
29+
30+
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 colorProfile from "./colorProfile";
1011
export * as colorize from "./colorize";
1112
export * as contrast from "./contrast";
1213
export * as crop from "../optionsShared/crop";

src/types/colorProfile.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* *Color Profile option*
3+
*
4+
* When set, imgproxy will convert the image's colorspace to the specified color profile
5+
* and embed the selected color profile in the output image.
6+
*
7+
* @note This is a Pro feature.
8+
* @note Ignored if the output format doesn't support color profiles.
9+
* @note Embedded profiles are not stripped by the standard color profile removal options.
10+
*
11+
* Available profiles:
12+
* - `srgb`: Built-in compact sRGB profile
13+
* - `cmyk`: Built-in "Chemical proof" CMYK profile
14+
* - Custom color profile filename (located in IMGPROXY_COLOR_PROFILES_DIR)
15+
*
16+
* @see {@link https://docs.imgproxy.net/generating_the_url?id=color-profile | color profile option imgproxy docs}
17+
*/
18+
type ColorProfile = string;
19+
20+
/**
21+
* *Color Profile option*
22+
*
23+
* To describe the Color Profile option, you can use the keywords `color_profile`, `cp`, or `icc`.
24+
*
25+
* @see https://docs.imgproxy.net/generating_the_url?id=color-profile
26+
*/
27+
interface ColorProfileOptionsPartial {
28+
color_profile?: ColorProfile;
29+
cp?: ColorProfile;
30+
icc?: ColorProfile;
31+
}
32+
33+
export { ColorProfile, ColorProfileOptionsPartial };

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 { ColorProfileOptionsPartial } from "./colorProfile";
1011
import type { ColorizeOptionsPartial } from "./colorize";
1112
import type { ContrastOptionsPartial } from "./contrast";
1213
import type { CropOptionsPartial } from "../typesShared/crop";
@@ -85,6 +86,7 @@ export type Options = AdjustOptionsPartial &
8586
BlurOptionsPartial &
8687
BrightnessOptionsPartial &
8788
CacheBusterOptionsPartial &
89+
ColorProfileOptionsPartial &
8890
ColorizeOptionsPartial &
8991
ContrastOptionsPartial &
9092
CropOptionsPartial &
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { describe, expect, it } from "vitest";
2+
import { test, build } from "../../src/options/colorProfile";
3+
4+
describe("colorProfile", () => {
5+
describe("test", () => {
6+
it("should return true if color_profile option is defined", () => {
7+
expect(test({ color_profile: "srgb" })).toEqual(true);
8+
});
9+
10+
it("should return true if cp option is defined", () => {
11+
expect(test({ cp: "cmyk" })).toEqual(true);
12+
});
13+
14+
it("should return true if icc option is defined", () => {
15+
expect(test({ icc: "custom_profile" })).toEqual(true);
16+
});
17+
18+
it("should return false if color_profile option is undefined", () => {
19+
expect(test({})).toEqual(false);
20+
});
21+
});
22+
23+
describe("build", () => {
24+
it("should throw an error if color_profile option is undefined", () => {
25+
expect(() => build({})).toThrow("color_profile option is undefined");
26+
});
27+
28+
it("should return 'cp:srgb' if color_profile option is 'srgb'", () => {
29+
expect(build({ color_profile: "srgb" })).toEqual("cp:srgb");
30+
});
31+
32+
it("should return 'cp:cmyk' if cp option is 'cmyk'", () => {
33+
expect(build({ cp: "cmyk" })).toEqual("cp:cmyk");
34+
});
35+
36+
it("should return 'cp:custom_profile' if icc option is 'custom_profile'", () => {
37+
expect(build({ icc: "custom_profile" })).toEqual("cp:custom_profile");
38+
});
39+
40+
it("should handle custom profile filename", () => {
41+
expect(build({ color_profile: "my-custom-profile" })).toEqual(
42+
"cp:my-custom-profile"
43+
);
44+
});
45+
46+
it("should handle percent-encoded filename", () => {
47+
expect(build({ cp: "profile%20with%20spaces" })).toEqual(
48+
"cp:profile%20with%20spaces"
49+
);
50+
});
51+
});
52+
});

0 commit comments

Comments
 (0)