From 9d1328c89d2b17c6f57943149a780619ce2ffd42 Mon Sep 17 00:00:00 2001 From: Reto Aebersold Date: Mon, 6 Nov 2023 09:11:36 +0100 Subject: [PATCH] fix: rating to color --- .../src/utils/__tests__/ratingToColor.spec.ts | 43 +++++++++++++++++++ client/src/utils/ratingToColor.ts | 7 ++- 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 client/src/utils/__tests__/ratingToColor.spec.ts diff --git a/client/src/utils/__tests__/ratingToColor.spec.ts b/client/src/utils/__tests__/ratingToColor.spec.ts new file mode 100644 index 00000000..7bbef770 --- /dev/null +++ b/client/src/utils/__tests__/ratingToColor.spec.ts @@ -0,0 +1,43 @@ +import { getBlendedColorForRating } from "@/utils/ratingToColor"; +import { describe, expect, it } from "vitest"; + +describe("getBlendedColorForRating", () => { + // Normal cases + it("should return red for rating 1", () => { + expect(getBlendedColorForRating(1)).toBe("rgb(221, 103, 81)"); + }); + + it("should return yellow for rating 2", () => { + expect(getBlendedColorForRating(2)).toBe("rgb(250, 200, 82)"); + }); + + it("should return light green for rating 3", () => { + expect(getBlendedColorForRating(3)).toBe("rgb(120, 222, 163)"); + }); + + it("should return dark green for rating 4", () => { + expect(getBlendedColorForRating(4)).toBe("rgb(91, 183, 130)"); + }); + + // Edge cases + it("should blend red and yellow for a rating between 1 and 2", () => { + expect(getBlendedColorForRating(1.5)).toBe("rgb(235, 151, 81)"); + }); + + it("should blend yellow and light green for a rating between 2 and 3", () => { + expect(getBlendedColorForRating(2.5)).toBe("rgb(185, 211, 122)"); + }); + + it("should blend light green and dark green for a rating between 3 and 4", () => { + expect(getBlendedColorForRating(3.5)).toBe("rgb(105, 202, 146)"); + }); + + // Ratings beyond the scale + it("should return dark green for ratings above 4", () => { + expect(getBlendedColorForRating(4.5)).toBe("rgb(91, 183, 130)"); + }); + + it("should return grey for ratings below 1", () => { + expect(getBlendedColorForRating(0.5)).toBe("rgb(237, 242, 246)"); + }); +}); diff --git a/client/src/utils/ratingToColor.ts b/client/src/utils/ratingToColor.ts index bd1b9e05..dc5a2917 100644 --- a/client/src/utils/ratingToColor.ts +++ b/client/src/utils/ratingToColor.ts @@ -1,5 +1,6 @@ type RGB = [number, number, number]; +const grey: RGB = [237, 242, 246]; // grey-200 const red: RGB = [221, 103, 81]; // red-600 const yellow: RGB = [250, 200, 82]; // yellow-500 const lightGreen: RGB = [120, 222, 163]; // green-500 @@ -30,6 +31,8 @@ export const getBlendedColorForRating = (rating: number): string => { let colors: [RGB, RGB]; switch (scale) { + case 0: + return getRGBString(grey); case 1: colors = [red, yellow]; break; @@ -37,9 +40,11 @@ export const getBlendedColorForRating = (rating: number): string => { colors = [yellow, lightGreen]; break; case 3: - default: colors = [lightGreen, darkGreen]; break; + case 4: + default: + return getRGBString(darkGreen); } const blendedColor = blendColors(colors[0], colors[1], weight);