-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add color transformation function by matrix
- Loading branch information
Showing
2 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters