From 3ccf5357ce698fbf964491c5be1c0e77011cbb4c Mon Sep 17 00:00:00 2001 From: Tim <47184194+imgde@users.noreply.github.com> Date: Sun, 15 Jun 2025 15:16:38 +0200 Subject: [PATCH] Add Admin Item Color grading --- .../src/helper/adminpanel/ItemsInfo.tsx | 57 +++++++++++++------ 1 file changed, 41 insertions(+), 16 deletions(-) diff --git a/01-frontend/src/helper/adminpanel/ItemsInfo.tsx b/01-frontend/src/helper/adminpanel/ItemsInfo.tsx index a480dcb..62f8dd1 100644 --- a/01-frontend/src/helper/adminpanel/ItemsInfo.tsx +++ b/01-frontend/src/helper/adminpanel/ItemsInfo.tsx @@ -5,7 +5,7 @@ import Item from "../../components/Item"; import {useTranslation} from "react-i18next"; import {DataGrid, GridColDef, GridRowId, GridRowSelectionModel} from "@mui/x-data-grid"; import {useEffect, useState} from "react"; -import {Gauge} from "@mui/x-charts"; +import {Gauge, gaugeClasses} from "@mui/x-charts"; export default function ItemsInfo() { const theme = useTheme(); @@ -19,14 +19,36 @@ export default function ItemsInfo() { const ratio = maxVal !== minVal ? (clamped - minVal) / (maxVal - minVal) : 0; + return hsvDegToHex(120 * ratio);//120° is green, 0° is red + } - const red = Math.floor(255 * (1 - ratio)); - const green = Math.floor(255 * ratio) - - const redHex = red.toString(16).padStart(2, '0').toUpperCase(); - const greenHex = green.toString(16).padStart(2, '0').toUpperCase(); - - return `#${redHex}${greenHex}00`; // Blue is always 00 + function hsvDegToHex(h: number): string { + h = Math.max(0, Math.min(360, h)); + + const c = 1; + const x = (1 - Math.abs(((h / 60) % 2) - 1)); + + let r = 0, g = 0, b = 0; + if (h < 60) { + r = c; g = x; b = 0; + } else if (h < 120) { + r = x; g = c; b = 0; + } else if (h < 180) { + r = 0; g = c; b = x; + } else if (h < 240) { + r = 0; g = x; b = c; + } else if (h < 300) { + r = x; g = 0; b = c; + } else { + r = c; g = 0; b = x; + } + + // scale to 0-255 + const rHex = Math.round(r * 255).toString(16).padStart(2, '0'); + const gHex = Math.round(g * 255).toString(16).padStart(2, '0'); + const bHex = Math.round(b * 255).toString(16).padStart(2, '0'); + + return `#${rHex}${gHex}${bHex}`; } function handleIconEdit(item: Item) { @@ -44,7 +66,7 @@ export default function ItemsInfo() { stock: 100, stockExpected: 1200, category: "garden", - rating: 5, + rating: 7, discount100: 21, }, { @@ -125,7 +147,11 @@ export default function ItemsInfo() { width: 100, editable: true, type: 'number', - renderCell: params => + renderCell: params => {return mapValueToColor(0, params.row.stockExpected, params.row.stock)}, + }, + }}/> }, { field: 'rating', @@ -133,7 +159,11 @@ export default function ItemsInfo() { width: 100, editable: true, type: 'number', - renderCell: params => + renderCell: params => {return mapValueToColor(0, 10, params.row.rating)}, + }, + }}/> }, { //edit billing information button field: "actualPrice", @@ -151,11 +181,6 @@ export default function ItemsInfo() { } ]; - useEffect(() => { - console.log(mapValueToColor(0,10,2)) - console.log(mapValueToColor(0,10,7)) - }, []); - return (