diff --git a/advanced_color_test.ts b/advanced_color_test.ts index 5a910a9..296a60d 100644 --- a/advanced_color_test.ts +++ b/advanced_color_test.ts @@ -3,12 +3,7 @@ import { assertEquals } from "./dev_deps.ts"; import { AdvancedColor } from "./advanced_color.ts"; import { Rgb } from "./color/mod.ts"; -import { Color } from "./color.ts"; - -export function truncComponents(color: AdvancedColor | Color): Rgb { - const [_red, _green, _blue]: Rgb = color.components; - return [Math.trunc(_red), Math.trunc(_green), Math.trunc(_blue)]; -} +import { getTruncComponents } from "./util.ts"; Deno.test("class contains correct fields", async (t) => { const red = 31; @@ -31,13 +26,13 @@ Deno.test("class contains correct fields", async (t) => { await t.step("dependent field contains correct values", () => { assertEquals( - truncComponents(color.rgbColor), + getTruncComponents(color.rgbColor), [red, green, blue], `"color.rgbColor"`, ); assertEquals( - truncComponents(color), + getTruncComponents(color), [red, green, blue], `"color"`, ); @@ -50,20 +45,20 @@ Deno.test("HSL conversion of red, green and blue correct", async (t) => { const blue: Rgb = [0, 0, 255]; const hue: AdvancedColor = new AdvancedColor(0); - assertEquals(truncComponents(hue), red, `"hue"`); + assertEquals(getTruncComponents(hue), red, `"hue"`); await t.step("no lightness or saturation values do not change hue", () => { const hueSaturation: AdvancedColor = new AdvancedColor(0, 100); - assertEquals(truncComponents(hueSaturation), red, `"hueSaturation"`); + assertEquals(getTruncComponents(hueSaturation), red, `"hueSaturation"`); const hueLightness: AdvancedColor = new AdvancedColor(0, undefined, 50); - assertEquals(truncComponents(hueLightness), red, `"hueLightness"`); + assertEquals(getTruncComponents(hueLightness), red, `"hueLightness"`); }); await t.step("default values do not change hue", () => { const hueSaturationLightness: AdvancedColor = new AdvancedColor(0, 100, 50); assertEquals( - truncComponents(hueSaturationLightness), + getTruncComponents(hueSaturationLightness), red, `"hueSaturationLightness"`, ); @@ -71,23 +66,23 @@ Deno.test("HSL conversion of red, green and blue correct", async (t) => { await t.step("hue range correct", () => { const startRedColor: AdvancedColor = new AdvancedColor(0); - assertEquals(truncComponents(startRedColor), red, `"startRedColor"`); + assertEquals(getTruncComponents(startRedColor), red, `"startRedColor"`); const greenColor: AdvancedColor = new AdvancedColor(33.33); - assertEquals(truncComponents(greenColor), green, `"greenColor"`); + assertEquals(getTruncComponents(greenColor), green, `"greenColor"`); const blueColor: AdvancedColor = new AdvancedColor(66.66); - assertEquals(truncComponents(blueColor), blue, `"blueColor"`); + assertEquals(getTruncComponents(blueColor), blue, `"blueColor"`); const endRedColor: AdvancedColor = new AdvancedColor(100); - assertEquals(truncComponents(endRedColor), red, `"endRedColor"`); + assertEquals(getTruncComponents(endRedColor), red, `"endRedColor"`); }); await t.step("saturation range correct", () => { const redSaturated: Rgb = [255, 0, 0]; const saturatedColor: AdvancedColor = new AdvancedColor(0, 100); assertEquals( - truncComponents(saturatedColor), + getTruncComponents(saturatedColor), redSaturated, `"saturatedColor"`, ); @@ -95,7 +90,7 @@ Deno.test("HSL conversion of red, green and blue correct", async (t) => { const redUnsaturated: Rgb = [63, 63, 63]; const unsaturatedColor: AdvancedColor = new AdvancedColor(0, 0); assertEquals( - truncComponents(unsaturatedColor), + getTruncComponents(unsaturatedColor), redUnsaturated, `"unsaturatedColor"`, ); @@ -105,7 +100,7 @@ Deno.test("HSL conversion of red, green and blue correct", async (t) => { const black: Rgb = [0, 0, 0]; const blackColor: AdvancedColor = new AdvancedColor(0, undefined, 0); assertEquals( - truncComponents(blackColor), + getTruncComponents(blackColor), black, `"blackColor"`, ); @@ -113,7 +108,7 @@ Deno.test("HSL conversion of red, green and blue correct", async (t) => { const white: Rgb = [255, 255, 255]; const whiteColor: AdvancedColor = new AdvancedColor(0, undefined, 100); assertEquals( - truncComponents(whiteColor), + getTruncComponents(whiteColor), white, `"whiteColor"`, ); diff --git a/color_effects/color_blind/mod_test.ts b/color_effects/color_blind/mod_test.ts index e89e932..e210308 100644 --- a/color_effects/color_blind/mod_test.ts +++ b/color_effects/color_blind/mod_test.ts @@ -3,7 +3,7 @@ import { Color } from "../../color.ts"; import * as originalFilters from "./mod.ts"; import { assertEquals } from "../../dev_deps.ts"; -import { truncComponents } from "../../advanced_color_test.ts"; +import { getTruncComponents } from "../../util.ts"; // deno-lint-ignore no-explicit-any const filters = originalFilters as any; @@ -59,31 +59,31 @@ Deno.test("color blindness functions correct", async (t) => { await t.step(`${func.name} function correct`, () => { assertEquals( - truncComponents(func(red)), + getTruncComponents(func(red)), functionValues?.[func.name]?.[0], `"truncComponents(func(red))"`, ); assertEquals( - truncComponents(func(green)), + getTruncComponents(func(green)), functionValues?.[func.name]?.[1], `"truncComponents(func(green))"`, ); assertEquals( - truncComponents(func(blue)), + getTruncComponents(func(blue)), functionValues?.[func.name]?.[2], `"truncComponents(func(blue))"`, ); assertEquals( - truncComponents(func(white)), + getTruncComponents(func(white)), [255, 255, 255], `"truncComponents(func(white))"`, ); assertEquals( - truncComponents(func(black)), + getTruncComponents(func(black)), [0, 0, 0], `"truncComponents(func(black))"`, ); diff --git a/color_effects/mix_colors_test.ts b/color_effects/mix_colors_test.ts index e00d3c0..5cbd29d 100644 --- a/color_effects/mix_colors_test.ts +++ b/color_effects/mix_colors_test.ts @@ -1,9 +1,9 @@ // Copyright 2023 mineejo. All rights reserved. MIT license. import { assertEquals } from "../dev_deps.ts"; -import { truncComponents } from "../advanced_color_test.ts"; import { Color } from "../color.ts"; import { mixColors } from "./mix_colors.ts"; +import { getTruncComponents } from "../util.ts"; Deno.test("mix colors function correct", async (t) => { const purple: Color = mixColors( @@ -12,7 +12,7 @@ Deno.test("mix colors function correct", async (t) => { 50, ); - assertEquals(truncComponents(purple), [127, 0, 127], `"purple"`); + assertEquals(getTruncComponents(purple), [127, 0, 127], `"purple"`); await t.step("percentage correct", () => { const blue: Color = mixColors( @@ -21,7 +21,7 @@ Deno.test("mix colors function correct", async (t) => { 100, ); - assertEquals(truncComponents(blue), [0, 0, 255], `"blue"`); + assertEquals(getTruncComponents(blue), [0, 0, 255], `"blue"`); const red: Color = mixColors( new Color(false, 255, 0, 0), @@ -29,7 +29,7 @@ Deno.test("mix colors function correct", async (t) => { 0, ); - assertEquals(truncComponents(red), [255, 0, 0], `"red"`); + assertEquals(getTruncComponents(red), [255, 0, 0], `"red"`); const black: Color = mixColors( new Color(false, 0, 0, 0), @@ -37,6 +37,6 @@ Deno.test("mix colors function correct", async (t) => { 50, ); - assertEquals(truncComponents(black), [0, 0, 0], `"black"`); + assertEquals(getTruncComponents(black), [0, 0, 0], `"black"`); }); }); diff --git a/util.ts b/util.ts index 9c64a09..b3840d7 100644 --- a/util.ts +++ b/util.ts @@ -1,5 +1,15 @@ // Copyright 2023 mineejo. All rights reserved. MIT license. +import { AdvancedColor } from "./advanced_color.ts"; +import { Color } from "./color.ts"; +import { Rgb } from "./color/rgb.ts"; + export function percent(value: number): number { return Math.min(Math.max(value, 0), 100); } + +export function getTruncComponents(color: AdvancedColor | Color): Rgb { + return color.components.map((component: number) => + Math.trunc(component) + ) as Rgb; +}