fix: rating to color

This commit is contained in:
Reto Aebersold 2023-11-06 09:11:36 +01:00
parent 26de615f22
commit 9d1328c89d
2 changed files with 49 additions and 1 deletions

View File

@ -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)");
});
});

View File

@ -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);