diff --git a/color_effects/matrix_color.ts b/color_effects/matrix_color.ts new file mode 100644 index 0000000..1977ee2 --- /dev/null +++ b/color_effects/matrix_color.ts @@ -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, + ); +} diff --git a/color_effects/mod.ts b/color_effects/mod.ts index 47499c6..1da386f 100644 --- a/color_effects/mod.ts +++ b/color_effects/mod.ts @@ -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";