From a79341271ba0423db7950ad5d92f1a3c875df322 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Tue, 3 Dec 2024 22:22:40 -0500 Subject: [PATCH] Implement color sliders --- .../3-log-density/FlameColor.tsx | 82 +++++++++++++++++-- .../3-log-density/color.ts | 2 - 2 files changed, 74 insertions(+), 10 deletions(-) diff --git a/blog/2024-11-15-playing-with-fire/3-log-density/FlameColor.tsx b/blog/2024-11-15-playing-with-fire/3-log-density/FlameColor.tsx index ea3aa49..0a80624 100644 --- a/blog/2024-11-15-playing-with-fire/3-log-density/FlameColor.tsx +++ b/blog/2024-11-15-playing-with-fire/3-log-density/FlameColor.tsx @@ -1,19 +1,81 @@ -import React, {useContext, useEffect, useState} from "react"; +import React, {useContext, useEffect, useMemo, useRef, useState} from "react"; import * as params from "../src/params"; import {PainterContext} from "../src/Canvas"; import {chaosGameColor} from "./chaosGameColor"; +import styles from "../src/css/styles.module.css"; +import {palette} from "../src/params"; + +const colorSwatch = (width: number, height: number, palette: number[], color: number): ImageData => { + return new ImageData(width, height); +} + +type ColorSliderProps = { + title: string; + palette: number[]; + color: number; + setColor: (color: number) => void; + children?: React.ReactNode; +} +const ColorSlider: React.FC = ({title, palette, color, setColor, children}) => { + const swatch = useMemo(() => colorSwatch(50, 20, palette, color), [palette, color]); + + return ( + <> +
+

{title}: {color}

+ setColor(Number(e.currentTarget.value))}/> +
+ {children} + + ) +} + +type PaletteBarProps = { + height: number; + palette: number[]; + sizingStyle: any; + children?: React.ReactNode; +} +const PaletteBar: React.FC = ({height, palette, sizingStyle, children}) => { + const sizingRef = useRef(null); + const [width, setWidth] = useState(0); + useEffect(() => { + if (!sizingRef.current) { + return; + } + + setWidth(sizingRef.current.offsetWidth); + }, [sizingRef.current]); + + const canvasRef = useRef(null); + useEffect(() => { + if (!canvasRef.current) { + return; + } + }, [canvasRef.current]); + + return ( +
+ ) +} + type Props = { quality?: number; children?: React.ReactElement; } -export default function FlameHistogram({quality, children}: Props) { +export default function FlameColor({quality, children}: Props) { const {width, height, setPainter} = useContext(PainterContext); - const [counter, setCount] = useState(0); const [xform1Color, setXform1Color] = useState(params.xform1Color); const [xform2Color, setXform2Color] = useState(params.xform2Color); const [xform3Color, setXform3Color] = useState(params.xform3Color); + const resetColor = () => { + setXform1Color(params.xform1Color); + setXform2Color(params.xform2Color); + setXform3Color(params.xform3Color); + } useEffect(() => { const gameParams = { @@ -26,12 +88,16 @@ export default function FlameHistogram({quality, children}: Props) { colors: [xform1Color, xform2Color, xform3Color] } setPainter(chaosGameColor(gameParams)); - }, [counter, xform1Color, xform2Color, xform3Color]); + }, [xform1Color, xform2Color, xform3Color]); + const resetButton = return ( - <> - {children} - - +
+

Transform Colors {resetButton}

+ + + + +
); } \ No newline at end of file diff --git a/blog/2024-11-15-playing-with-fire/3-log-density/color.ts b/blog/2024-11-15-playing-with-fire/3-log-density/color.ts index 983a059..eb1c094 100644 --- a/blog/2024-11-15-playing-with-fire/3-log-density/color.ts +++ b/blog/2024-11-15-playing-with-fire/3-log-density/color.ts @@ -1,5 +1,3 @@ -import {histIndex} from "../src/camera"; - export function colorFromPalette(palette: number[], colorIndex: number): [number, number, number] { const paletteIndex = Math.floor(colorIndex * (palette.length / 3)) * 3; return [palette[paletteIndex], palette[paletteIndex + 1], palette[paletteIndex + 2]];