Skip to content
The Chat Archive
gpt-4.1-mini-2025-04-14· OpenAI LMArena Human Preference 140K

Create a web interface for a color picker app that allows selecting different color…

  1. User

    Create a web interface for a color picker app that allows selecting different color spaces and displays color wheels or other models depedning on the color space. The color hovered over should be displayed in various formats.

  2. Assistant

    Certainly! Below is a complete React-based web interface for a color picker app that:

    • Allows selecting different color spaces (RGB, HSL, HSV, and CIELAB).
    • Displays an appropriate color wheel or model for the selected color space.
    • Shows the color hovered over in various formats (HEX, RGB, HSL, HSV, and CIELAB).
    • Updates the displayed color info dynamically as you hover over the color wheel.

    Features:

    • Color Spaces: RGB, HSL, HSV, CIELAB
    • Color Wheels:
      • RGB: 2D RGB cube slice (simplified)
      • HSL: Hue circle with saturation and lightness sliders
      • HSV: Hue circle with saturation and value sliders
      • CIELAB: 2D slice of a* and b* with fixed L (lightness)
    • Color Info: HEX, RGB, HSL, HSV, CIELAB (converted using color-convert library)
    • Hover Interaction: Shows color under mouse pointer on the wheel

    How to run:

    • Save the code below in a file ColorPickerApp.jsx
    • Run in a React environment (e.g., Create React App)
    • Install dependencies: npm install color-convert

    Code:

    import React, { useState, useRef, useEffect } from "react";
    import convert from "color-convert";
    
    const WIDTH = 300;
    const HEIGHT = 300;
    
    const colorSpaces = ["RGB", "HSL", "HSV", "CIELAB"];
    
    function rgbToHex(r, g, b) {
      return "#" + [r, g, b].map((x) => x.toString(16).padStart(2, "0")).join("");
    }
    
    function clamp(num, min, max) {
      return Math.min(Math.max(num, min), max);
    }
    
    function drawRGB(ctx) {
      // Draw a simple RGB gradient square (red-green on axes, blue fixed)
      const size = WIDTH;
      const blue = 128;
      const imageData = ctx.createImageData(size, size);
      for (let y = 0; y < size; y++) {
        for (let x = 0; x < size; x++) {
          const r = Math.floor((x / (size - 1)) * 255);
          const g = Math.floor((y / (size - 1)) * 255);
          const b = blue;
          const idx = (y * size + x) * 4;
          imageData.data[idx] = r;
          imageData.data[idx + 1] = g;
          imageData.data[idx + 2] = b;
          imageData.data[idx + 3] = 255;
        }
      }
      ctx.putImageData(imageData, 0, 0);
    }
    
    function drawHSL(ctx) {
      // Draw hue circle with saturation and lightness fixed at 0.5
      const radius = WIDTH / 2;
      const center = radius;
      const lightness = 0.5;
    
      const imageData = ctx.createImageData(WIDTH, HEIGHT);
      for (let y = 0; y < HEIGHT; y++) {
        for (let x = 0; x < WIDTH; x++) {
          const dx = x - center;
          const dy = y - center;
          const dist = Math.sqrt(dx * dx + dy * dy);
          if (dist > radius) {
            // Transparent outside circle
            const idx = (y * WIDTH + x) * 4;
            imageData.data[idx + 3] = 0;
            continue;
          }
          let hue = (Math.atan2(dy, dx) * 180) / Math.PI;
          if (hue < 0) hue += 360;
          const saturation = dist / radius;
    
          const [r, g, b] = convert.hsl.rgb([hue, saturation * 100, lightness * 100]);
          const idx = (y * WIDTH + x) * 4;
          imageData.data[idx] = r;
          imageData.data[idx + 1] = g;
          imageData.data[idx + 2] = b;
          imageData.data[idx + 3] = 255;
        }
      }
      ctx.putImageData(imageData, 0, 0);
    }
    
    function drawHSV(ctx) {
      // Draw hue circle with saturation and value fixed at 1
      const radius = WIDTH / 2;
      const center = radius;
      const value = 1;
    
      const imageData = ctx.createImageData(WIDTH, HEIGHT);
      for (let y = 0; y < HEIGHT; y++) {
        for (let x = 0; x < WIDTH; x++) {
          const dx = x - center;
          const dy = y - center;
          const dist = Math.sqrt(dx * dx + dy * dy);
          if (dist > radius) {
            // Transparent outside circle
            const idx = (y * WIDTH + x) * 4;
            imageData.data[idx + 3] = 0;
            continue;
          }
          let hue = (Math.atan2(dy, dx) * 180) / Math.PI;
          if (hue < 0) hue += 360;
          const saturation = dist / radius;
    
          const [r, g, b] = convert.hsv.rgb([hue, saturation * 100, value * 100]);
          const idx = (y * WIDTH + x) * 4;
          imageData.data[idx] = r;
          imageData.data[idx + 1] = g;
          imageData.data[idx + 2] = b;
          imageData.data[idx + 3] = 255;
        }
      }
      ctx.putImageData(imageData, 0, 0);
    }
    
    function drawCIELAB(ctx) {
      // Draw a* and b* plane with fixed L=50
      // a* and b* range approx -128 to 127, map to canvas
      const size = WIDTH;
      const L = 50;
    
      const imageData = ctx.createImageData(size, size);
      for (let y = 0; y < size; y++) {
        for (let x = 0; x < size; x++) {
          const a = ((x / (size - 1)) * 255) - 128;
          const b = 128 - (y / (size - 1)) * 255;
    
          // Convert LAB to RGB
          let [r, g, bl] = convert.lab.rgb([L, a, b]);
          r = clamp(r, 0, 255);
          g = clamp(g, 0, 255);
          bl = clamp(bl, 0, 255);
    
          const idx = (y * size + x) * 4;
          imageData.data[idx] = r;
          imageData.data[idx + 1] = g;
          imageData.data[idx + 2] = bl;
          imageData.data[idx + 3] = 255;
        }
      }
      ctx.putImageData(imageData, 0, 0);
    }
    
    function ColorPickerApp() {
      const [colorSpace, setColorSpace] = useState("HSL");
      const [hoverColor, setHoverColor] = useState(null);
      const canvasRef = useRef(null);
    
      useEffect(() => {
        const ctx = canvasRef.current.getContext("2d");
        ctx.clearRect(0, 0, WIDTH, HEIGHT);
        switch (colorSpace) {
          case "RGB":
            drawRGB(ctx);
            break;
          case "HSL":
            drawHSL(ctx);
            break;
          case "HSV":
            drawHSV(ctx);
            break;
          case "CIELAB":
            drawCIELAB(ctx);
            break;
          default:
            break;
        }
        setHoverColor(null);
      }, [colorSpace]);
    
      function handleMouseMove(e) {
        const rect = canvasRef.current.getBoundingClientRect();
        const x = e.clientX - rect.left;
        const y = e.clientY - rect.top;
    
        if (x < 0 || y < 0 || x >= WIDTH || y >= HEIGHT) {
          setHoverColor(null);
          return;
        }
    
        const ctx = canvasRef.current.getContext("2d");
        const pixel = ctx.getImageData(x, y, 1, 1).data;
        if (pixel[3] === 0) {
          setHoverColor(null);
          return;
        }
    
        const r = pixel[0];
        const g = pixel[1];
        const b = pixel[2];
    
        // Convert to other formats
        const hex = rgbToHex(r, g, b);
        const rgb = [r, g, b];
        const hsl = convert.rgb.hsl(rgb);
        const hsv = convert.rgb.hsv(rgb);
        const lab = convert.rgb.lab(rgb);
    
        setHoverColor({ hex, rgb, hsl, hsv, lab });
      }
    
      function handleMouseLeave() {
        setHoverColor(null);
      }
    
      return (
        <div style={{ fontFamily: "Arial, sans-serif", maxWidth: 400, margin: "auto" }}>
          <h2>Color Picker App</h2>
          <label>
            Select Color Space:{" "}
            <select
              value={colorSpace}
              onChange={(e) => setColorSpace(e.target.value)}
              style={{ fontSize: 16, padding: 4 }}
            >
              {colorSpaces.map((cs) => (
                <option key={cs} value={cs}>
                  {cs}
                </option>
              ))}
            </select>
          </label>
          <div style={{ marginTop: 20, position: "relative" }}>
            <canvas
              ref={canvasRef}
              width={WIDTH}
              height={HEIGHT}
              style={{ border: "1px solid #ccc", borderRadius: 8, cursor: "crosshair" }}
              onMouseMove={handleMouseMove}
              onMouseLeave={handleMouseLeave}
            />
          </div>
          {hoverColor ? (
            <div
              style={{
                marginTop: 20,
                padding: 10,
                border: "1px solid #ddd",
                borderRadius: 8,
                backgroundColor: "#f9f9f9",
              }}
            >
              <div
                style={{
                  width: 50,
                  height: 50,
                  backgroundColor: hoverColor.hex,
                  border: "1px solid #ccc",
                  borderRadius: 4,
                  marginBottom: 10,
                }}
              />
              <div><b>HEX:</b> {hoverColor.hex.toUpperCase()}</div>
              <div><b>RGB:</b> {hoverColor.rgb.join(", ")}</div>
              <div><b>HSL:</b> {hoverColor.hsl.map((v) => v.toFixed(1)).join(", ")}</div>
              <div><b>HSV:</b> {hoverColor.hsv.map((v) => v.toFixed(1)).join(", ")}</div>
              <div><b>CIELAB:</b> {hoverColor.lab.map((v) => v.toFixed(1)).join(", ")}</div>
            </div>
          ) : (
            <div style={{ marginTop: 20, color: "#666" }}>Hover over the color wheel to see color info.</div>
          )}
        </div>
      );
    }
    
    export default ColorPickerApp;
    

    Explanation:

    • The app uses a <canvas> to draw different color wheels/models depending on the selected color space.
    • On mouse move, it reads the pixel color under the cursor and converts it to multiple color formats using the color-convert library.
    • The color info is displayed below the canvas with a color preview box.
    • The CIELAB model is shown as a 2D slice of a* and b* with fixed L=50.
    • RGB shows a simple red-green gradient with blue fixed at 128 for demonstration.
    • HSL and HSV show hue circles with saturation as radius and fixed lightness/value.
    • The interface is minimal and responsive.

    If you want me to provide a vanilla JS/HTML version or add more color spaces/models, just ask!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.