{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"hue-folds","type":"registry:ui","title":"Hue Folds","description":"Smooth ribbons of color that bend, fold and drift; the cursor gently warps them like a lens. WebGL.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/hue-folds.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport interface HueFoldsProps {\n  /** Up to four colors the bands flow through. */\n  colors?: string[];\n  /** Flow speed multiplier; 0 freezes the motion. */\n  speed?: number;\n  /** Zoom; higher means wider bends. */\n  scale?: number;\n  /** Number of bands across the view. */\n  bands?: number;\n  /** How strongly the cursor warps the bands (0 disables). */\n  warp?: number;\n  className?: string;\n}\n\nconst VERT = \"attribute vec2 p;void main(){gl_Position=vec4(p,0.,1.);}\";\n\nconst FRAG = `\nprecision highp float;\nuniform vec2 uRes;\nuniform float uTime;\nuniform vec2 uMouse;\nuniform float uActive;\nuniform vec3 uC0;\nuniform vec3 uC1;\nuniform vec3 uC2;\nuniform vec3 uC3;\nuniform float uScale;\nuniform float uBands;\nuniform float uWarp;\n\n// Ping-pongs through the colors (0 1 2 3 2 1 0) so no muddy wrap-around blend appears.\nvec3 palette(float t) {\n  t = (1.0 - abs(fract(t * 0.5) * 2.0 - 1.0)) * 3.0;\n  vec3 c = mix(uC0, uC1, smoothstep(0.0, 1.0, clamp(t, 0.0, 1.0)));\n  c = mix(c, uC2, smoothstep(0.0, 1.0, clamp(t - 1.0, 0.0, 1.0)));\n  return mix(c, uC3, smoothstep(0.0, 1.0, clamp(t - 2.0, 0.0, 1.0)));\n}\n\n// Band coordinate: a slanted gradient bent by layered, drifting sines.\nfloat field(vec2 p, float t) {\n  p.y += 0.42 * sin(p.x * 0.9 + t * 0.6);\n  p.x += 0.32 * sin(p.y * 1.4 - t * 0.45);\n  p.y += 0.24 * sin(p.x * 2.1 + p.y * 0.7 + t * 0.35);\n  p.x += 0.12 * sin(p.y * 3.1 + t * 0.5);\n  return p.y * 0.9 + p.x * 0.35;\n}\n\nvoid main() {\n  vec2 p = (gl_FragCoord.xy - 0.5 * uRes) / uRes.y * 2.0 / uScale;\n  vec2 m = (uMouse - 0.5) * uRes / uRes.y * 2.0 / uScale;\n  vec2 d = p - m;\n  p -= d * uWarp * 0.45 * exp(-dot(d, d) * 2.5) * uActive;\n\n  float t = uTime * 0.4;\n  float s = field(p, t);\n  float e = 0.004;\n  vec2 grad = vec2(field(p + vec2(e, 0.0), t) - s, field(p + vec2(0.0, e), t) - s) / e;\n\n  float k = s * uBands;\n  float ridge = max(sin(fract(k) * 3.14159), 0.0);\n  vec3 col = palette(k * 0.16 + t * 0.04);\n\n  // Each band reads as a soft ribbon lit from above; compressed folds fall into shadow.\n  float light = 0.5 + 0.5 * dot(normalize(grad + 1e-4), normalize(vec2(-0.35, 0.94)));\n  col *= (0.5 + 0.5 * pow(ridge, 0.7)) * (0.72 + 0.4 * light);\n  col += vec3(1.0) * pow(ridge, 26.0) * 0.22 * light;\n  col *= mix(1.0, 0.45, smoothstep(1.3, 2.6, length(grad)));\n\n  vec2 v = gl_FragCoord.xy / uRes - 0.5;\n  col *= 1.0 - dot(v, v) * 0.8;\n  gl_FragColor = vec4(col, 1.0);\n}\n`;\n\nfunction rgb(hex: string) {\n  let h = hex.replace(\"#\", \"\");\n  if (h.length === 3) h = h.replace(/./g, \"$&$&\");\n  const n = parseInt(h.padEnd(6, \"0\").slice(0, 6), 16) || 0;\n  return [((n >> 16) & 255) / 255, ((n >> 8) & 255) / 255, (n & 255) / 255];\n}\n\n/** Compiles a full-screen-triangle program and returns a cached uniform lookup, or null. */\nfunction fullscreen(gl: WebGLRenderingContext, frag: string) {\n  const prog = gl.createProgram();\n  if (!prog) return null;\n  for (const [type, src] of [\n    [gl.VERTEX_SHADER, VERT],\n    [gl.FRAGMENT_SHADER, frag],\n  ] as const) {\n    const sh = gl.createShader(type);\n    if (!sh) return null;\n    gl.shaderSource(sh, src);\n    gl.compileShader(sh);\n    if (!gl.getShaderParameter(sh, gl.COMPILE_STATUS)) console.warn(gl.getShaderInfoLog(sh));\n    gl.attachShader(prog, sh);\n  }\n  gl.bindAttribLocation(prog, 0, \"p\");\n  gl.linkProgram(prog);\n  if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) return null;\n  gl.useProgram(prog);\n  gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());\n  gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);\n  gl.enableVertexAttribArray(0);\n  gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);\n  const cache = new Map<string, WebGLUniformLocation | null>();\n  return (name: string) => {\n    if (!cache.has(name)) cache.set(name, gl.getUniformLocation(prog, name));\n    return cache.get(name) ?? null;\n  };\n}\n\nexport function HueFolds({\n  colors = [\"#ff5e3a\", \"#ff2d95\", \"#7a5cff\", \"#00c2ff\"],\n  speed = 1,\n  scale = 1,\n  bands = 3,\n  warp = 1,\n  className,\n}: HueFoldsProps) {\n  const host = useRef<HTMLDivElement>(null);\n  const opts = useRef({ colors, speed, scale, bands, warp });\n  const redraw = useRef<(() => void) | null>(null);\n\n  useEffect(() => {\n    opts.current = { colors, speed, scale, bands, warp };\n    redraw.current?.();\n  }, [colors, speed, scale, bands, warp]);\n\n  useEffect(() => {\n    const el = host.current!;\n    const canvas = document.createElement(\"canvas\");\n    canvas.style.cssText = \"position:absolute;inset:0;width:100%;height:100%;display:block\";\n    el.appendChild(canvas);\n    const gl = canvas.getContext(\"webgl\", { antialias: false, powerPreference: \"low-power\" });\n    const u = gl && fullscreen(gl, FRAG);\n    if (!gl || !u) {\n      canvas.remove();\n      return;\n    }\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const pointer = { x: 0.5, y: 0.5, active: 0, tx: 0.5, ty: 0.5, ta: 0 };\n    let raf = 0;\n    let visible = true;\n    let time = 4;\n    let last = 0;\n\n    const draw = () => {\n      const o = opts.current;\n      const list = o.colors.length ? o.colors : [\"#ffffff\"];\n      for (let i = 0; i < 4; i++) gl.uniform3fv(u(`uC${i}`), rgb(list[i % list.length]));\n      gl.uniform2f(u(\"uRes\"), canvas.width, canvas.height);\n      gl.uniform1f(u(\"uTime\"), time);\n      gl.uniform2f(u(\"uMouse\"), pointer.x, pointer.y);\n      gl.uniform1f(u(\"uActive\"), pointer.active);\n      gl.uniform1f(u(\"uScale\"), Math.max(o.scale, 0.05));\n      gl.uniform1f(u(\"uBands\"), Math.max(o.bands, 0.1));\n      gl.uniform1f(u(\"uWarp\"), o.warp);\n      gl.drawArrays(gl.TRIANGLES, 0, 3);\n    };\n    const loop = (now: number) => {\n      time += (Math.min(now - last, 50) / 1000) * opts.current.speed;\n      last = now;\n      pointer.x += (pointer.tx - pointer.x) * 0.06;\n      pointer.y += (pointer.ty - pointer.y) * 0.06;\n      pointer.active += (pointer.ta - pointer.active) * 0.04;\n      draw();\n      raf = requestAnimationFrame(loop);\n    };\n    const play = () => {\n      if (raf || !visible || reduced) return;\n      last = performance.now();\n      raf = requestAnimationFrame(loop);\n    };\n    const resize = () => {\n      const dpr = Math.min(window.devicePixelRatio || 1, 2);\n      canvas.width = Math.max(1, Math.round(el.clientWidth * dpr));\n      canvas.height = Math.max(1, Math.round(el.clientHeight * dpr));\n      gl.viewport(0, 0, canvas.width, canvas.height);\n      draw();\n    };\n    const onMove = (e: PointerEvent) => {\n      const r = el.getBoundingClientRect();\n      pointer.tx = (e.clientX - r.left) / r.width;\n      pointer.ty = 1 - (e.clientY - r.top) / r.height;\n      if (!pointer.ta) {\n        pointer.x = pointer.tx;\n        pointer.y = pointer.ty;\n      }\n      pointer.ta = 1;\n    };\n    const onLeave = () => {\n      pointer.ta = 0;\n    };\n\n    const target = el.parentElement ?? el;\n    target.addEventListener(\"pointermove\", onMove, { passive: true });\n    target.addEventListener(\"pointerleave\", onLeave);\n    const ro = new ResizeObserver(resize);\n    ro.observe(el);\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      if (visible) play();\n      else {\n        cancelAnimationFrame(raf);\n        raf = 0;\n      }\n    });\n    io.observe(el);\n    redraw.current = draw;\n\n    return () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      target.removeEventListener(\"pointermove\", onMove);\n      target.removeEventListener(\"pointerleave\", onLeave);\n      redraw.current = null;\n      gl.getExtension(\"WEBGL_lose_context\")?.loseContext();\n      canvas.remove();\n    };\n  }, []);\n\n  return (\n    <div\n      ref={host}\n      aria-hidden\n      className={className}\n      style={{ position: \"absolute\", inset: 0, overflow: \"hidden\", pointerEvents: \"none\" }}\n    />\n  );\n}\n"}]}