-
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.
- Loading branch information
1 parent
b6021e6
commit e1c31d5
Showing
10 changed files
with
163 additions
and
9 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
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,44 @@ | ||
import {describe, expect, test} from 'vitest'; | ||
import {SRGB, rgb255} from '.'; | ||
import {hslBlend, hwbBlend, rgbBlend} from './blend'; | ||
|
||
const COL_A = rgb255(39, 0, 214); | ||
const COL_B = rgb255(247, 148, 89); | ||
|
||
describe('rgbBlend', () => { | ||
test.each([ | ||
[new SRGB(0, 0, 0, 0), new SRGB(1, 1, 1, 1), 0.5, new SRGB(0.5, 0.5, 0.5, 0.5)], | ||
[new SRGB(1, 1, 1, 1), new SRGB(0, 0, 0, 0), 0.5, new SRGB(0.5, 0.5, 0.5, 0.5)], | ||
[new SRGB(0, 0, 0, 0), new SRGB(1, 1, 1, 1), 0.25, new SRGB(0.25, 0.25, 0.25, 0.25)], | ||
[COL_A, COL_B, 0, COL_A], | ||
[COL_A, COL_B, 0.25, rgb255(91, 37, 183, 1)], | ||
[COL_A, COL_B, 0.5, rgb255(143, 74, 152)], | ||
[COL_A, COL_B, 0.75, rgb255(195, 111, 120)], | ||
[COL_A, COL_B, 1, COL_B], | ||
] as [SRGB, SRGB, number, SRGB][])('rgbBlend(%s, %s, %d) -> %s', (a, b, f, expected) => { | ||
expect(rgbBlend(a, b, f).toString('hex')).toEqual(expected.toString('hex')); | ||
}); | ||
}); | ||
|
||
describe('hslBlend', () => { | ||
test.each([ | ||
[COL_A, COL_B, 0, COL_A], | ||
[COL_A, COL_B, 0.25, rgb255(3, 187, 242)], | ||
[COL_A, COL_B, 0.5, rgb255(25, 250, 88)], | ||
[COL_A, COL_B, 0.75, rgb255(186, 248, 58)], | ||
[COL_A, COL_B, 1, COL_B], | ||
] as [SRGB, SRGB, number, SRGB][])('hslBlend(%s, %s, %d) -> %s', (a, b, f, expected) => { | ||
expect(hslBlend(a, b, f).toString('hex')).toEqual(expected.toString('hex')); | ||
}); | ||
}); | ||
describe('blendMode - HWB', () => { | ||
test.each([ | ||
[COL_A, COL_B, 0, COL_A], | ||
[COL_A, COL_B, 0.25, rgb255(22, 176, 222)], | ||
[COL_A, COL_B, 0.5, rgb255(45, 230, 96)], | ||
[COL_A, COL_B, 0.75, rgb255(183, 239, 67)], | ||
[COL_A, COL_B, 1, COL_B], | ||
] as [SRGB, SRGB, number, SRGB][])('hwbBlend(%s, %s, %d) -> %s', (a, b, f, expected) => { | ||
expect(hwbBlend(a, b, f).toString('hex')).toEqual(expected.toString('hex')); | ||
}); | ||
}); |
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,39 @@ | ||
import {SRGB} from './srgb'; | ||
import {hslToSRGB, hwbToSRGB, SRGBToHsl, SRGBToHwb} from './utils'; | ||
|
||
export function rgbBlend(colorA: SRGB, colorB: SRGB, f: number): SRGB { | ||
return new SRGB( | ||
colorA.r + f * (colorB.r - colorA.r), | ||
colorA.g + f * (colorB.g - colorA.g), | ||
colorA.b + f * (colorB.b - colorA.b), | ||
colorA.a + f * (colorB.a - colorA.a) | ||
); | ||
} | ||
|
||
export function hslBlend(colorA: SRGB, colorB: SRGB, f: number): SRGB { | ||
const colorAHSL = SRGBToHsl(colorA.r, colorA.g, colorA.b); | ||
const colorBHSL = SRGBToHsl(colorB.r, colorB.g, colorB.b); | ||
|
||
return new SRGB( | ||
...hslToSRGB( | ||
colorAHSL[0] + f * (colorBHSL[0] - colorAHSL[0]), | ||
colorAHSL[1] + f * (colorBHSL[1] - colorAHSL[1]), | ||
colorAHSL[2] + f * (colorBHSL[2] - colorAHSL[2]) | ||
), | ||
colorA.a + f * (colorB.a - colorA.a) | ||
); | ||
} | ||
|
||
export function hwbBlend(colorA: SRGB, colorB: SRGB, f: number): SRGB { | ||
const colorAHWB = SRGBToHwb(colorA.r, colorA.g, colorA.b); | ||
const colorBHWB = SRGBToHwb(colorB.r, colorB.g, colorB.b); | ||
|
||
return new SRGB( | ||
...hwbToSRGB( | ||
colorAHWB[0] + f * (colorBHWB[0] - colorAHWB[0]), | ||
colorAHWB[1] + f * (colorBHWB[1] - colorAHWB[1]), | ||
colorAHWB[2] + f * (colorBHWB[2] - colorAHWB[2]) | ||
), | ||
colorA.a + f * (colorB.a - colorA.a) | ||
); | ||
} |
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
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
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,12 @@ | ||
import {SRGBBlendMode} from './colorspaces'; | ||
import {ColorSpace} from './css'; | ||
|
||
export type BlendMode = SRGBBlendMode; | ||
|
||
export const BlendModeColorSpaceMap: {[key in BlendMode]: ColorSpace} = { | ||
sRGB: 'sRGB', | ||
hex: 'sRGB', | ||
rgb: 'sRGB', | ||
hsl: 'sRGB', | ||
hwb: 'sRGB', | ||
}; |
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 @@ | ||
export * from './srgb'; |
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
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import {SRGBFormat} from './srgb'; | ||
import {SRGBBlendMode} from './colorspaces'; | ||
|
||
export type Format = SRGBFormat; | ||
export type Format = SRGBBlendMode; |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from './angle'; | ||
export * from './blend'; | ||
export * from './colorspaces'; | ||
export {CSSColor, ColorSpace} from './css'; | ||
export * from './format'; | ||
export * from './percentage'; | ||
export * from './srgb'; |