Skip to content

Commit

Permalink
feat: add color transformation function by matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
mineejo committed Nov 23, 2023
1 parent f169b89 commit 56b0fbe
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
35 changes: 35 additions & 0 deletions color_effects/matrix_color.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2023 mineejo. All rights reserved. MIT license.

import { Rgb } from "../color/mod.ts";
import { Color } from "../color.ts";

// deno-fmt-ignore
export type ColorTransformationMatrix = [
number, number, number,
number, number, number,
number, number, number
];

export function matrixColor(
color: Color,
matrix: ColorTransformationMatrix,
): Color {
const [red, green, blue]: Rgb = color.components.map((component: number) =>
component / 255
) as Rgb;

let index = 0;

return new Color(
false,
...new Array(3).fill(undefined).map(() => {
const component: number =
(matrix[index] * red + matrix[index + 1] * green +
matrix[index + 2] * blue) *
255;

index += 3;
return component;
}) as Rgb,
);
}
1 change: 1 addition & 0 deletions color_effects/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

export { discolor } from "./discolor.ts";
export { invertColor } from "./invert_color.ts";
export { matrixColor } from "./matrix_color.ts";
export { mixColors } from "./mix_colors.ts";
export { subtractColors } from "./subtract_colors.ts";
export { summarizeColors } from "./summarize_colors.ts";

0 comments on commit 56b0fbe

Please sign in to comment.