Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Munsell Color Solid</title>
- <style>
- @import url('https://fonts.googleapis.com/css2?family=Newsreader:ital,opsz,wght@0,6..72,400;0,6..72,500;1,6..72,400&family=IBM+Plex+Mono:wght@400;500;600&display=swap');
- :root{
- --bg: #0c0c10;
- --panel: rgba(20,20,26,0.82);
- --panel-border: #2c2c36;
- --ink: #e9e6df;
- --ink-dim: #9a978f;
- --line: #33333e;
- --accentA: #d0d0d0;
- --accentB: #d0d0d0;
- }
- *{ box-sizing: border-box; }
- html,body{
- margin:0; padding:0; width:100%; height:100%;
- background: var(--bg);
- color: var(--ink);
- font-family: 'Newsreader', serif;
- overflow: hidden;
- }
- #scene{ position:absolute; inset:0; display:block; }
- .mono{ font-family:'IBM Plex Mono', monospace; letter-spacing:0.02em; }
- #title{
- position:absolute; top:20px; left:24px; z-index:5;
- pointer-events:none;
- }
- #title h1{
- margin:0; font-size:22px; font-weight:500; font-style:italic;
- color:var(--ink);
- }
- #title p{
- margin:2px 0 0; font-size:11px; color:var(--ink-dim);
- font-family:'IBM Plex Mono',monospace; letter-spacing:0.04em;
- text-transform:uppercase;
- }
- #controlsBar{
- position:absolute; top:20px; right:24px; z-index:5;
- display:flex; gap:8px; align-items:center;
- flex-wrap:wrap; justify-content:flex-end;
- max-width: 60vw;
- }
- .pill{
- background: var(--panel);
- border: 1px solid var(--panel-border);
- color: var(--ink-dim);
- font-family:'IBM Plex Mono',monospace;
- font-size:11px;
- padding:7px 12px;
- border-radius: 3px;
- cursor:pointer;
- letter-spacing:0.03em;
- transition: all .15s ease;
- backdrop-filter: blur(6px);
- }
- .pill:hover{ color:var(--ink); border-color:#4a4a58; }
- .pill.active{ color:#0c0c10; background: var(--ink); border-color: var(--ink); }
- .pill.mini{ padding:5px 9px; font-size:10px; }
- .barDivider{ width:1px; height:18px; background: var(--panel-border); margin:0 2px; }
- .sizeCtl{ display:flex; align-items:center; gap:7px; }
- .sizeCtl span{ font-size:10px; color:var(--ink-dim); white-space:nowrap; }
- .sizeCtl input[type=range]{ width:64px; }
- .panel{
- position:absolute; bottom:24px; z-index:5;
- width: 300px;
- background: var(--panel);
- border: 1px solid var(--panel-border);
- border-radius: 4px;
- padding: 16px 18px 18px;
- backdrop-filter: blur(8px);
- }
- #panelA{ left:24px; }
- #panelB{ right:24px; }
- .panel-head{
- display:flex; align-items:center; gap:10px; margin-bottom:12px;
- }
- .swatch{
- width:38px; height:38px; border-radius:3px;
- border:1px solid rgba(255,255,255,0.15);
- box-shadow: inset 0 0 12px rgba(0,0,0,0.25);
- flex-shrink:0;
- }
- .panel-head .label{
- font-family:'IBM Plex Mono',monospace;
- font-size:10px; color:var(--ink-dim); text-transform:uppercase; letter-spacing:0.08em;
- }
- .panel-head .notation{
- font-family:'IBM Plex Mono',monospace;
- font-size:15px; color:var(--ink); font-weight:500;
- }
- .row{ margin-bottom:11px; }
- .row:last-child{ margin-bottom:0; }
- .row-top{
- display:flex; justify-content:space-between;
- font-family:'IBM Plex Mono',monospace;
- font-size:10px; color:var(--ink-dim); margin-bottom:4px;
- text-transform:uppercase; letter-spacing:0.06em;
- }
- .row-top span.val{ color: var(--ink); }
- input[type=range]{
- -webkit-appearance:none; width:100%; height:2px;
- background: var(--line); border-radius:2px; outline:none;
- margin:0;
- }
- input[type=range]::-webkit-slider-thumb{
- -webkit-appearance:none; width:12px; height:12px; border-radius:50%;
- background: var(--ink); cursor:pointer;
- border: 2px solid var(--bg);
- box-shadow: 0 0 0 1px var(--ink);
- }
- input[type=range]::-moz-range-thumb{
- width:12px; height:12px; border-radius:50%;
- background: var(--ink); cursor:pointer; border: 2px solid var(--bg);
- }
- #pathInfo{
- position:absolute; bottom:24px; left:50%; transform:translateX(-50%);
- z-index:5; text-align:center;
- background: var(--panel);
- border: 1px solid var(--panel-border);
- border-radius: 4px;
- padding: 10px 22px;
- font-family:'IBM Plex Mono',monospace;
- backdrop-filter: blur(8px);
- display:none;
- }
- #pathInfo .d{ font-size:10px; color:var(--ink-dim); text-transform:uppercase; letter-spacing:0.06em; margin-top:8px; }
- #pathInfo .v{ font-size:14px; color:var(--ink); margin-top:2px; }
- #pathTypeGroup{ display:flex; gap:6px; justify-content:center; }
- .scrubRow{ display:flex; align-items:center; gap:9px; margin-top:10px; }
- .scrubRow input[type=range]{ flex:1; }
- #onPathHead{ margin-top:12px; margin-bottom:0; padding-top:10px; border-top:1px solid var(--line); justify-content:center; }
- #hint{
- position:absolute; top:64px; left:24px; z-index:4;
- font-family:'IBM Plex Mono',monospace; font-size:10px; color:var(--ink-dim);
- letter-spacing:0.03em; opacity:0.7;
- }
- @media (max-width: 820px){
- .panel{ width: calc(50% - 32px); padding:12px 12px 14px; }
- #title h1{ font-size:17px; }
- #hint{ display:none; }
- .row-top{ font-size:9px; }
- }
- </style>
- </head>
- <body>
- <canvas id="scene"></canvas>
- <div id="title">
- <h1>The Munsell Color Solid</h1>
- <p>Hue · Value · Chroma — an approximate perceptual rendering</p>
- </div>
- <div id="hint">drag to orbit · scroll to zoom</div>
- <div id="controlsBar">
- <button class="pill" id="btnSolid">solid</button>
- <button class="pill" id="btnWire">wire</button>
- <button class="pill active" id="btnPoints">points</button>
- <div class="pill sizeCtl"><span>pt size</span><input type="range" id="ptSize" min="0.03" max="0.24" step="0.005" value="0.13"></div>
- <div class="barDivider"></div>
- <button class="pill" id="btnRotate">auto-rotate</button>
- <button class="pill active" id="btnPath">trace path</button>
- </div>
- <div class="panel" id="panelA">
- <div class="panel-head">
- <div class="swatch" id="swatchA"></div>
- <div>
- <div class="label">Color A</div>
- <div class="notation" id="notationA">5.0R 5.0/10.0</div>
- </div>
- </div>
- <div class="row">
- <div class="row-top"><span>Hue</span><span class="val" id="hueLabelA">5.0R</span></div>
- <input type="range" id="hueA" min="0" max="100" step="0.1" value="5">
- </div>
- <div class="row">
- <div class="row-top"><span>Value</span><span class="val" id="valLabelA">5.0</span></div>
- <input type="range" id="valA" min="0" max="10" step="0.1" value="5">
- </div>
- <div class="row">
- <div class="row-top"><span>Chroma</span><span class="val" id="chrLabelA">10.0</span></div>
- <input type="range" id="chrA" min="0" max="20" step="0.1" value="10">
- </div>
- </div>
- <div class="panel" id="panelB">
- <div class="panel-head">
- <div class="swatch" id="swatchB"></div>
- <div>
- <div class="label">Color B</div>
- <div class="notation" id="notationB">75.0PB 4.0/10.0</div>
- </div>
- </div>
- <div class="row">
- <div class="row-top"><span>Hue</span><span class="val" id="hueLabelB">75.0PB</span></div>
- <input type="range" id="hueB" min="0" max="100" step="0.1" value="75">
- </div>
- <div class="row">
- <div class="row-top"><span>Value</span><span class="val" id="valLabelB">4.0</span></div>
- <input type="range" id="valB" min="0" max="10" step="0.1" value="4">
- </div>
- <div class="row">
- <div class="row-top"><span>Chroma</span><span class="val" id="chrLabelB">10.0</span></div>
- <input type="range" id="chrB" min="0" max="20" step="0.1" value="10">
- </div>
- </div>
- <div id="pathInfo">
- <div id="pathTypeGroup">
- <button class="pill mini active" data-type="munsell">Munsell</button>
- <button class="pill mini" data-type="paint">Paint mix</button>
- <button class="pill mini" data-type="digital">Digital RGB</button>
- <button class="pill mini" data-type="linear">Linear</button>
- </div>
- <div class="d" id="pathCaption">Interpolated directly through hue, value & chroma</div>
- <div class="v" id="pathDist">—</div>
- <div class="scrubRow">
- <button class="pill mini active" id="btnPlayPause">pause</button>
- <input type="range" id="pathScrub" min="0" max="100" step="0.5" value="0">
- </div>
- <div class="panel-head" id="onPathHead">
- <div class="swatch" id="swatchPath"></div>
- <div>
- <div class="label">On path</div>
- <div class="notation" id="notationPath">—</div>
- </div>
- </div>
- </div>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
- <script>
- (function(){
- /* ---------------------------------------------------------------
- MUNSELL APPROXIMATION MODEL
- Ten principal hue families, evenly spaced 36° apart:
- R, YR, Y, GY, G, BG, B, PB, P, RP
- Each family has an approximate value at which chroma peaks,
- and an approximate maximum chroma at that peak. This produces
- the characteristic irregular "potato" shaped color solid rather
- than a uniform cylinder or sphere.
- ------------------------------------------------------------------*/
- const FAMILIES = ['R','YR','Y','GY','G','BG','B','PB','P','RP'];
- // peak value (0-10) and peak chroma (relative) per family
- const PEAK_VALUE = [4.5, 6.0, 8.2, 7.2, 5.2, 5.0, 4.2, 3.6, 3.6, 4.2];
- const PEAK_CHROMA = [15, 12, 13, 10, 8.5, 7.0, 8.5, 13, 11, 14 ];
- // perceptual hue-wheel degree that each family roughly corresponds to,
- // used only for coloring (Munsell hue spacing is NOT perceptually uniform
- // in RGB terms, so this remaps the 0-360 munsell angle to an approximate
- // display hue angle)
- const DISPLAY_DEG = [0, 28, 58, 95, 132, 168, 210, 255, 288, 325, 360];
- function wrapIndex(i){ return ((i % 10) + 10) % 10; }
- // Given a continuous munsell angle 0-360, interpolate peakValue/peakChroma
- function familyInterp(angleDeg, arr){
- const pos = ((angleDeg % 360) + 360) % 360 / 36; // 0-10 continuous
- const i0 = Math.floor(pos);
- const t = pos - i0;
- const i1 = wrapIndex(i0 + 1);
- const a = arr[wrapIndex(i0)];
- const b = arr[i1];
- // smoothstep interpolation for a gentler blend between families
- const s = t*t*(3-2*t);
- return a + (b-a)*s;
- }
- function maxChromaAt(angleDeg, value){
- const peakV = familyInterp(angleDeg, PEAK_VALUE);
- const peakC = familyInterp(angleDeg, PEAK_CHROMA);
- const sigma = 3.1;
- const bell = Math.exp(-((value-peakV)*(value-peakV))/(2*sigma*sigma));
- const envelope = Math.sin(Math.PI * Math.min(Math.max(value,0),10) / 10); // ->0 at V=0 and V=10
- return Math.max(0, peakC * bell * envelope);
- }
- function displayHueDeg(angleDeg){
- const pos = ((angleDeg % 360) + 360) % 360 / 36;
- const i0 = Math.floor(pos);
- const t = pos - i0;
- const a = DISPLAY_DEG[i0];
- const b = DISPLAY_DEG[i0+1];
- return a + (b-a)*t;
- }
- // inverse of displayHueDeg: given an RGB-wheel hue degree, recover the
- // approximate munsell angle (used to plot RGB-space interpolations back
- // inside the munsell coordinate system)
- function inverseDisplayHueDeg(displayDeg){
- const d = ((displayDeg%360)+360)%360;
- for(let i=0;i<10;i++){
- const a = DISPLAY_DEG[i], b = DISPLAY_DEG[i+1];
- if(d>=a && d<=b){
- const t = (b-a)>0 ? (d-a)/(b-a) : 0;
- return i*36 + t*36;
- }
- }
- return d;
- }
- function rgbToHsl(r,g,b){
- const max = Math.max(r,g,b), min = Math.min(r,g,b);
- let h,s;
- const l = (max+min)/2;
- if(max===min){ h=0; s=0; }
- else{
- const d = max-min;
- s = l>0.5 ? d/(2-max-min) : d/(max+min);
- switch(max){
- case r: h = (g-b)/d + (g<b?6:0); break;
- case g: h = (b-r)/d + 2; break;
- default: h = (r-g)/d + 4;
- }
- h *= 60;
- }
- return [h,s,l];
- }
- function hslToRgb(h,s,l){
- h = ((h%360)+360)%360/360;
- let r,g,b;
- if(s===0){ r=g=b=l; }
- else{
- const hue2rgb=(p,q,t)=>{
- if(t<0) t+=1; if(t>1) t-=1;
- if(t<1/6) return p+(q-p)*6*t;
- if(t<1/2) return q;
- if(t<2/3) return p+(q-p)*(2/3-t)*6;
- return p;
- };
- const q = l<0.5 ? l*(1+s) : l+s-l*s;
- const p = 2*l-q;
- r = hue2rgb(p,q,h+1/3);
- g = hue2rgb(p,q,h);
- b = hue2rgb(p,q,h-1/3);
- }
- return [r,g,b];
- }
- // hue: 0-360 munsell angle, value: 0-10, chroma: 0-max
- function munsellToRgb(hueDeg, value, chroma){
- const maxC = maxChromaAt(hueDeg, value);
- const sat = maxC > 0.0001 ? Math.min(chroma/maxC, 1) * 0.92 : 0;
- const l = Math.min(Math.max(value/10, 0), 1);
- const dh = displayHueDeg(hueDeg);
- return hslToRgb(dh, sat, l);
- }
- function rgbToHex(rgb){
- const c = rgb.map(v=>Math.round(Math.min(Math.max(v,0),1)*255));
- return '#'+c.map(v=>v.toString(16).padStart(2,'0')).join('');
- }
- // Convert (hueDeg 0-360, value 0-10, chroma 0-max) -> cartesian scene coords
- function munsellToXYZ(hueDeg, value, chroma){
- const rad = hueDeg * Math.PI/180;
- const scale = 0.34; // chroma units -> scene units
- const x = Math.cos(rad) * chroma * scale;
- const z = Math.sin(rad) * chroma * scale;
- const y = (value - 5) * 0.85;
- return new THREE.Vector3(x,y,z);
- }
- function xyzToMunsell(pos){
- const scale = 0.34;
- const value = pos.y/0.85 + 5;
- const radius = Math.sqrt(pos.x*pos.x + pos.z*pos.z);
- const chroma = radius/scale;
- let hue = Math.atan2(pos.z, pos.x) * 180/Math.PI;
- if(hue<0) hue += 360;
- return {hue, value, chroma};
- }
- // hue slider (0-100) -> munsell angle (0-360) and notation label
- function hueSliderToDeg(v){ return (v/100)*360; }
- function formatNotationFromAngle(angleDeg, value, chroma){
- const famPos = ((angleDeg%360)+360)%360/36;
- const fi = Math.floor(famPos+1e-9) % 10;
- const sub = (famPos - fi) * 10;
- return `${sub.toFixed(1)}${FAMILIES[fi]} ${value.toFixed(1)}/${chroma.toFixed(1)}`;
- }
- function notationLabel(hueSlider, value, chroma){
- return formatNotationFromAngle(hueSliderToDeg(hueSlider), value, chroma);
- }
- /* ---------------------------------------------------------------
- THREE.JS SCENE SETUP
- ------------------------------------------------------------------*/
- const canvas = document.getElementById('scene');
- const renderer = new THREE.WebGLRenderer({canvas, antialias:true, alpha:false});
- renderer.setPixelRatio(Math.min(window.devicePixelRatio,2));
- const scene = new THREE.Scene();
- scene.background = new THREE.Color(0x0c0c10);
- scene.fog = new THREE.Fog(0x0c0c10, 14, 30);
- const camera = new THREE.PerspectiveCamera(42, window.innerWidth/window.innerHeight, 0.1, 100);
- // simple orbit control implemented manually
- let camTheta = Math.PI*0.28, camPhi = Math.PI*0.38, camRadius = 11;
- const camTarget = new THREE.Vector3(0,0,0);
- function updateCamera(){
- const p = Math.min(Math.max(camPhi, 0.12), Math.PI-0.12);
- camera.position.set(
- camTarget.x + camRadius*Math.sin(p)*Math.sin(camTheta),
- camTarget.y + camRadius*Math.cos(p),
- camTarget.z + camRadius*Math.sin(p)*Math.cos(camTheta)
- );
- camera.lookAt(camTarget);
- }
- updateCamera();
- let dragging=false, lastX=0, lastY=0;
- canvas.addEventListener('pointerdown', e=>{ dragging=true; lastX=e.clientX; lastY=e.clientY; autoRotate=false; setRotateBtn(); });
- window.addEventListener('pointerup', ()=> dragging=false);
- window.addEventListener('pointermove', e=>{
- if(!dragging) return;
- const dx = e.clientX-lastX, dy=e.clientY-lastY;
- lastX=e.clientX; lastY=e.clientY;
- camTheta -= dx*0.0055;
- camPhi -= dy*0.0055;
- updateCamera();
- });
- canvas.addEventListener('wheel', e=>{
- e.preventDefault();
- camRadius = Math.min(Math.max(camRadius + e.deltaY*0.01, 4), 24);
- updateCamera();
- }, {passive:false});
- // touch pinch / drag support (basic)
- let lastTouchDist=null;
- canvas.addEventListener('touchstart', e=>{
- if(e.touches.length===1){ dragging=true; lastX=e.touches[0].clientX; lastY=e.touches[0].clientY; autoRotate=false; setRotateBtn(); }
- else if(e.touches.length===2){
- lastTouchDist = Math.hypot(e.touches[0].clientX-e.touches[1].clientX, e.touches[0].clientY-e.touches[1].clientY);
- }
- }, {passive:true});
- canvas.addEventListener('touchmove', e=>{
- if(e.touches.length===1 && dragging){
- const dx=e.touches[0].clientX-lastX, dy=e.touches[0].clientY-lastY;
- lastX=e.touches[0].clientX; lastY=e.touches[0].clientY;
- camTheta -= dx*0.006; camPhi -= dy*0.006;
- updateCamera();
- } else if(e.touches.length===2 && lastTouchDist){
- const d = Math.hypot(e.touches[0].clientX-e.touches[1].clientX, e.touches[0].clientY-e.touches[1].clientY);
- camRadius = Math.min(Math.max(camRadius + (lastTouchDist-d)*0.02, 4), 24);
- lastTouchDist = d;
- updateCamera();
- }
- }, {passive:true});
- canvas.addEventListener('touchend', ()=>{ dragging=false; lastTouchDist=null; });
- function resize(){
- const w = window.innerWidth, h = window.innerHeight;
- renderer.setSize(w,h);
- camera.aspect = w/h;
- camera.updateProjectionMatrix();
- }
- window.addEventListener('resize', resize);
- resize();
- // lighting
- scene.add(new THREE.AmbientLight(0xffffff, 0.55));
- const key = new THREE.DirectionalLight(0xffffff, 0.9);
- key.position.set(6,10,8);
- scene.add(key);
- const fill = new THREE.DirectionalLight(0x8899ff, 0.25);
- fill.position.set(-8,-4,-6);
- scene.add(fill);
- /* ---------------------------------------------------------------
- BUILD THE COLOR SOLID (outer chroma envelope surface)
- ------------------------------------------------------------------*/
- const VALUE_STEPS = 48;
- const HUE_STEPS = 64;
- function buildSolidGeometry(){
- const positions = [];
- const colors = [];
- const indices = [];
- const grid = [];
- for(let vi=0; vi<=VALUE_STEPS; vi++){
- const value = (vi/VALUE_STEPS)*10;
- const row = [];
- for(let hi=0; hi<=HUE_STEPS; hi++){
- const angle = (hi/HUE_STEPS)*360;
- const chroma = maxChromaAt(angle, value);
- const pos = munsellToXYZ(angle, value, chroma);
- const rgb = munsellToRgb(angle, value, chroma);
- positions.push(pos.x, pos.y, pos.z);
- colors.push(rgb[0], rgb[1], rgb[2]);
- row.push(positions.length/3 - 1);
- }
- grid.push(row);
- }
- for(let vi=0; vi<VALUE_STEPS; vi++){
- for(let hi=0; hi<HUE_STEPS; hi++){
- const a = grid[vi][hi], b = grid[vi][hi+1];
- const c = grid[vi+1][hi], d = grid[vi+1][hi+1];
- indices.push(a,c,b, b,c,d);
- }
- }
- const geo = new THREE.BufferGeometry();
- geo.setAttribute('position', new THREE.Float32BufferAttribute(positions,3));
- geo.setAttribute('color', new THREE.Float32BufferAttribute(colors,3));
- geo.setIndex(indices);
- geo.computeVertexNormals();
- return geo;
- }
- const solidGeo = buildSolidGeometry();
- const solidMat = new THREE.MeshPhongMaterial({
- vertexColors:true, shininess:8, specular:0x222222,
- side:THREE.DoubleSide
- });
- const solidMesh = new THREE.Mesh(solidGeo, solidMat);
- solidMesh.visible = false;
- scene.add(solidMesh);
- // wireframe overlay (meridians + parallels), hidden by default
- const wireGeo = new THREE.EdgesGeometry(solidGeo, 1);
- const wireMat = new THREE.LineBasicMaterial({color:0x000000, transparent:true, opacity:0.18});
- const wireMesh = new THREE.LineSegments(wireGeo, wireMat);
- wireMesh.visible = false;
- scene.add(wireMesh);
- // points mode: a volumetric fill (not just the outer shell) so the
- // desaturated colors living inside the solid are visible too.
- function buildPointCloudGeometry(){
- const HUE_N = 40;
- const VALUE_N = 27;
- const CHROMA_N = 9; // radial samples from center out to the max-chroma shell
- const positions = [];
- const colors = [];
- for(let vi=0; vi<=VALUE_N; vi++){
- const value = (vi/VALUE_N)*10;
- for(let hi=0; hi<HUE_N; hi++){
- const angle = (hi/HUE_N)*360;
- const maxC = maxChromaAt(angle, value);
- for(let ci=0; ci<=CHROMA_N; ci++){
- // skip repeating the center point for every hue slice
- if(ci===0 && hi>0) continue;
- const frac = ci/CHROMA_N;
- const chroma = frac*maxC;
- const pos = munsellToXYZ(angle, value, chroma);
- const rgb = munsellToRgb(angle, value, chroma);
- positions.push(pos.x,pos.y,pos.z);
- colors.push(rgb[0],rgb[1],rgb[2]);
- }
- }
- }
- const geo = new THREE.BufferGeometry();
- geo.setAttribute('position', new THREE.Float32BufferAttribute(positions,3));
- geo.setAttribute('color', new THREE.Float32BufferAttribute(colors,3));
- return geo;
- }
- const cloudGeo = buildPointCloudGeometry();
- const pointsMat = new THREE.PointsMaterial({size:0.13, vertexColors:true, sizeAttenuation:true, transparent:true, opacity:0.92});
- const pointsMesh = new THREE.Points(cloudGeo, pointsMat);
- pointsMesh.visible = true;
- scene.add(pointsMesh);
- document.getElementById('ptSize').addEventListener('input', function(){
- pointsMat.size = parseFloat(this.value);
- });
- /* central neutral axis: black at bottom, white at top */
- (function(){
- const segs = 32;
- const pos = [], col = [];
- for(let i=0;i<=segs;i++){
- const v = (i/segs)*10;
- pos.push(0, (v-5)*0.85, 0);
- const l = v/10;
- col.push(l,l,l);
- }
- const geo = new THREE.BufferGeometry();
- geo.setAttribute('position', new THREE.Float32BufferAttribute(pos,3));
- geo.setAttribute('color', new THREE.Float32BufferAttribute(col,3));
- const mat = new THREE.LineBasicMaterial({vertexColors:true, linewidth:2});
- scene.add(new THREE.Line(geo, mat));
- // small ticks
- for(let v=0; v<=10; v+=2){
- const l = v/10;
- const tickGeo = new THREE.SphereGeometry(0.035, 10, 10);
- const tickMat = new THREE.MeshBasicMaterial({color: new THREE.Color(l,l,l)});
- const tick = new THREE.Mesh(tickGeo, tickMat);
- tick.position.set(0,(v-5)*0.85,0);
- scene.add(tick);
- }
- })();
- /* text sprite helper */
- function makeTextSprite(text, opts={}){
- const fontSize = opts.fontSize || 64;
- const color = opts.color || '#e9e6df';
- const canvasEl = document.createElement('canvas');
- const ctx = canvasEl.getContext('2d');
- ctx.font = `500 ${fontSize}px 'IBM Plex Mono', monospace`;
- const metrics = ctx.measureText(text);
- const pad = fontSize*0.4;
- canvasEl.width = metrics.width + pad*2;
- canvasEl.height = fontSize*1.5;
- ctx.font = `500 ${fontSize}px 'IBM Plex Mono', monospace`;
- ctx.fillStyle = color;
- ctx.textBaseline = 'middle';
- ctx.fillText(text, pad, canvasEl.height/2);
- const tex = new THREE.CanvasTexture(canvasEl);
- tex.needsUpdate = true;
- const mat = new THREE.SpriteMaterial({map:tex, transparent:true, depthTest:false});
- const sprite = new THREE.Sprite(mat);
- const scale = (opts.scale || 0.5);
- sprite.scale.set(scale * canvasEl.width/canvasEl.height, scale, 1);
- sprite.renderOrder = 999;
- return sprite;
- }
- // hue family labels around the equator (value = 5 ring, just outside max chroma)
- FAMILIES.forEach((name, i)=>{
- const angle = i*36;
- const value = 5;
- const chroma = maxChromaAt(angle, value) + 2.4;
- const p = munsellToXYZ(angle, value, chroma);
- const label = makeTextSprite(name, {fontSize:56, color:'#c9c6bd', scale:0.42});
- label.position.copy(p);
- scene.add(label);
- });
- // value labels along the axis
- [0,2,4,6,8,10].forEach(v=>{
- const label = makeTextSprite('V'+v, {fontSize:48, color:'#7d7a72', scale:0.3});
- label.position.set(0.55, (v-5)*0.85, 0);
- scene.add(label);
- });
- /* ---------------------------------------------------------------
- COLOR A / COLOR B markers + path
- ------------------------------------------------------------------*/
- const markerAGeo = new THREE.SphereGeometry(0.14, 24, 24);
- const markerBGeo = new THREE.SphereGeometry(0.14, 24, 24);
- const markerAMat = new THREE.MeshBasicMaterial({color:0xffffff});
- const markerBMat = new THREE.MeshBasicMaterial({color:0xffffff});
- const markerA = new THREE.Mesh(markerAGeo, markerAMat);
- const markerB = new THREE.Mesh(markerBGeo, markerBMat);
- const ringGeoA = new THREE.RingGeometry(0.18,0.22,32);
- const ringGeoB = new THREE.RingGeometry(0.18,0.22,32);
- const ringMatA = new THREE.MeshBasicMaterial({color:0xffffff, side:THREE.DoubleSide, transparent:true, opacity:0.85});
- const ringMatB = new THREE.MeshBasicMaterial({color:0xffffff, side:THREE.DoubleSide, transparent:true, opacity:0.85});
- const ringA = new THREE.Mesh(ringGeoA, ringMatA);
- const ringB = new THREE.Mesh(ringGeoB, ringMatB);
- scene.add(markerA, markerB, ringA, ringB);
- let pathGroup = new THREE.Group();
- scene.add(pathGroup);
- let cometMesh = null;
- let cometCurvePoints = [];
- let pathA = null, pathB = null;
- let pathProgress = 0, pathDirection = 1, pathPlaying = true;
- function shortestAngleLerp(a,b,t){
- let diff = ((b-a+540)%360)-180;
- return a + diff*t;
- }
- const PATH_CAPTIONS = {
- munsell: 'Interpolated directly through hue, value & chroma',
- paint: 'Dips toward muddy gray, as complementary pigments do when physically mixed',
- digital: 'Straight blend of RGB pixel values, as a gradient tool would draw it',
- linear: 'A straight line through the 3-D space itself — true complements cross the gray axis'
- };
- function angularDist(a,b){
- return Math.abs(((b-a+540)%360)-180);
- }
- // sample point + color for a given interpolation type at parameter t
- function samplePath(A,B,t,type){
- if(type==='munsell'){
- const h = shortestAngleLerp(A.hue, B.hue, t);
- const v = A.value + (B.value-A.value)*t;
- const c = A.chroma + (B.chroma-A.chroma)*t;
- return {pos: munsellToXYZ(h,v,c), rgb: munsellToRgb(h,v,c), hue:h, value:v, chroma:c};
- }
- if(type==='paint'){
- // real pigments mixed together tend to desaturate toward a muddy
- // neutral in the middle, more so the further apart the hues are.
- const ang = angularDist(A.hue,B.hue);
- const dip = 0.15 + 0.65*(ang/180);
- const bell = Math.sin(Math.PI*t);
- const h = shortestAngleLerp(A.hue, B.hue, t);
- let v = A.value + (B.value-A.value)*t;
- v -= 0.9*bell*(ang/180); // mixtures often run slightly darker/duller
- v = Math.min(Math.max(v,0),10);
- const cBase = A.chroma + (B.chroma-A.chroma)*t;
- const c = Math.max(cBase*(1-dip*bell), 0);
- return {pos: munsellToXYZ(h,v,c), rgb: munsellToRgb(h,v,c), hue:h, value:v, chroma:c};
- }
- if(type==='linear'){
- const posA = munsellToXYZ(A.hue, A.value, A.chroma);
- const posB = munsellToXYZ(B.hue, B.value, B.chroma);
- const pos = posA.clone().lerp(posB, t);
- const m = xyzToMunsell(pos);
- return {pos, rgb: munsellToRgb(m.hue, m.value, m.chroma), hue:m.hue, value:m.value, chroma:m.chroma};
- }
- // digital: linear blend in RGB space (as a screen gradient tool would do),
- // then approximately re-plotted back into munsell coordinates
- const rgbA = munsellToRgb(A.hue,A.value,A.chroma);
- const rgbB = munsellToRgb(B.hue,B.value,B.chroma);
- const r = rgbA[0]+(rgbB[0]-rgbA[0])*t;
- const g = rgbA[1]+(rgbB[1]-rgbA[1])*t;
- const b = rgbA[2]+(rgbB[2]-rgbA[2])*t;
- const [hh,ss,ll] = rgbToHsl(r,g,b);
- const munsellAngle = inverseDisplayHueDeg(hh);
- const value = ll*10;
- const maxC = maxChromaAt(munsellAngle, value);
- const chroma = Math.min((ss/0.92)*maxC, maxC);
- return {pos: munsellToXYZ(munsellAngle, value, chroma), rgb:[r,g,b], hue:munsellAngle, value:value, chroma:chroma};
- }
- function rebuildPath(A,B, visible){
- pathGroup.clear();
- if(!visible) {
- document.getElementById('pathInfo').style.display='none';
- pathA = null; pathB = null;
- return;
- }
- document.getElementById('pathInfo').style.display='block';
- document.getElementById('pathCaption').innerHTML = PATH_CAPTIONS[pathType];
- pathA = A; pathB = B;
- const N = 60;
- const pts = [];
- const cols = [];
- for(let i=0;i<=N;i++){
- const t = i/N;
- const sample = samplePath(A,B,t,pathType);
- pts.push(sample.pos);
- cols.push(sample.rgb);
- }
- cometCurvePoints = pts;
- // line with vertex colors
- const positions = [];
- const colorsArr = [];
- pts.forEach((p,i)=>{
- positions.push(p.x,p.y,p.z);
- colorsArr.push(cols[i][0],cols[i][1],cols[i][2]);
- });
- const lineGeo = new THREE.BufferGeometry();
- lineGeo.setAttribute('position', new THREE.Float32BufferAttribute(positions,3));
- lineGeo.setAttribute('color', new THREE.Float32BufferAttribute(colorsArr,3));
- const lineMat = new THREE.LineBasicMaterial({vertexColors:true, linewidth:2});
- pathGroup.add(new THREE.Line(lineGeo, lineMat));
- // small bead markers along path for thickness illusion
- for(let i=0;i<=N;i+=4){
- const beadGeo = new THREE.SphereGeometry(0.045, 10, 10);
- const rgb = cols[i];
- const beadMat = new THREE.MeshBasicMaterial({color: new THREE.Color(rgb[0],rgb[1],rgb[2])});
- const bead = new THREE.Mesh(beadGeo, beadMat);
- bead.position.copy(pts[i]);
- pathGroup.add(bead);
- }
- // traveling comet — its color is set every frame to match the color
- // it currently sits on (see animate())
- const cometGeo = new THREE.SphereGeometry(0.1, 20, 20);
- const cometMat = new THREE.MeshBasicMaterial({color:0xffffff});
- cometMesh = new THREE.Mesh(cometGeo, cometMat);
- pathGroup.add(cometMesh);
- // distance readout (perceptual-ish: cylindrical euclidean distance in scene units, rescaled)
- const startP = pts[0], endP = pts[pts.length-1];
- const dist = startP.distanceTo(endP) / 0.34; // back into chroma-ish units, rough
- const straightDist = Math.sqrt(
- Math.pow(A.value-B.value,2)*4 +
- Math.pow(dist,2)
- );
- document.getElementById('pathDist').textContent = straightDist.toFixed(2) + ' units (\u0394V, \u0394H, \u0394C combined)';
- }
- /* ---------------------------------------------------------------
- UI WIRING
- ------------------------------------------------------------------*/
- function readColor(prefix){
- const hueSlider = parseFloat(document.getElementById('hue'+prefix).value);
- const value = parseFloat(document.getElementById('val'+prefix).value);
- let chroma = parseFloat(document.getElementById('chr'+prefix).value);
- const hue = hueSliderToDeg(hueSlider);
- const maxC = maxChromaAt(hue, value);
- chroma = Math.min(chroma, Math.max(maxC, 0.5));
- return {hueSlider, hue, value, chroma, maxC};
- }
- function famLabel(hueSlider){
- const angle = hueSliderToDeg(hueSlider);
- const famPos = ((angle%360)+360)%360/36;
- const fi = Math.floor(famPos+1e-9) % 10;
- const sub = (famPos - fi) * 10;
- return sub.toFixed(1) + FAMILIES[fi];
- }
- let showPath = true;
- let viewMode = 'points';
- let pathType = 'munsell';
- let autoRotate = false;
- function refresh(){
- const A = readColor('A');
- const B = readColor('B');
- document.getElementById('chrA').max = Math.max(A.maxC, 1).toFixed(1);
- document.getElementById('chrB').max = Math.max(B.maxC, 1).toFixed(1);
- document.getElementById('hueLabelA').textContent = famLabel(A.hueSlider);
- document.getElementById('valLabelA').textContent = A.value.toFixed(1);
- document.getElementById('chrLabelA').textContent = A.chroma.toFixed(1);
- document.getElementById('hueLabelB').textContent = famLabel(B.hueSlider);
- document.getElementById('valLabelB').textContent = B.value.toFixed(1);
- document.getElementById('chrLabelB').textContent = B.chroma.toFixed(1);
- const rgbA = munsellToRgb(A.hue, A.value, A.chroma);
- const rgbB = munsellToRgb(B.hue, B.value, B.chroma);
- document.getElementById('swatchA').style.background = rgbToHex(rgbA);
- document.getElementById('swatchB').style.background = rgbToHex(rgbB);
- document.getElementById('notationA').textContent = notationLabel(A.hueSlider, A.value, A.chroma);
- document.getElementById('notationB').textContent = notationLabel(B.hueSlider, B.value, B.chroma);
- const posA = munsellToXYZ(A.hue, A.value, A.chroma);
- const posB = munsellToXYZ(B.hue, B.value, B.chroma);
- markerA.position.copy(posA);
- markerB.position.copy(posB);
- ringA.position.copy(posA);
- ringB.position.copy(posB);
- markerAMat.color.setRGB(rgbA[0],rgbA[1],rgbA[2]);
- markerBMat.color.setRGB(rgbB[0],rgbB[1],rgbB[2]);
- rebuildPath(A,B, showPath);
- return {A,B};
- }
- ['hueA','valA','chrA','hueB','valB','chrB'].forEach(id=>{
- document.getElementById(id).addEventListener('input', refresh);
- });
- document.getElementById('btnPath').addEventListener('click', function(){
- showPath = !showPath;
- this.classList.toggle('active', showPath);
- refresh();
- });
- document.querySelectorAll('#pathTypeGroup .pill').forEach(btn=>{
- btn.addEventListener('click', function(){
- pathType = this.getAttribute('data-type');
- document.querySelectorAll('#pathTypeGroup .pill').forEach(b=>b.classList.remove('active'));
- this.classList.add('active');
- refresh();
- });
- });
- const swatchPathEl = document.getElementById('swatchPath');
- const notationPathEl = document.getElementById('notationPath');
- const pathScrubEl = document.getElementById('pathScrub');
- const btnPlayPauseEl = document.getElementById('btnPlayPause');
- btnPlayPauseEl.addEventListener('click', function(){
- pathPlaying = !pathPlaying;
- this.textContent = pathPlaying ? 'pause' : 'play';
- this.classList.toggle('active', pathPlaying);
- });
- pathScrubEl.addEventListener('input', function(){
- pathPlaying = false;
- btnPlayPauseEl.textContent = 'play';
- btnPlayPauseEl.classList.remove('active');
- pathProgress = parseFloat(this.value)/100;
- });
- function setViewMode(mode){
- viewMode = mode;
- solidMesh.visible = (mode==='solid');
- wireMesh.visible = (mode==='wire');
- pointsMesh.visible = (mode==='points');
- ['btnSolid','btnWire','btnPoints'].forEach(id=>document.getElementById(id).classList.remove('active'));
- document.getElementById('btn'+mode[0].toUpperCase()+mode.slice(1)).classList.add('active');
- }
- document.getElementById('btnSolid').addEventListener('click', ()=>setViewMode('solid'));
- document.getElementById('btnWire').addEventListener('click', ()=>setViewMode('wire'));
- document.getElementById('btnPoints').addEventListener('click', ()=>setViewMode('points'));
- function setRotateBtn(){
- document.getElementById('btnRotate').classList.toggle('active', autoRotate);
- }
- document.getElementById('btnRotate').addEventListener('click', function(){
- autoRotate = !autoRotate;
- setRotateBtn();
- });
- refresh();
- /* ---------------------------------------------------------------
- ANIMATION LOOP
- ------------------------------------------------------------------*/
- const clock = new THREE.Clock();
- function animate(){
- requestAnimationFrame(animate);
- const dt = clock.getDelta();
- const t = clock.getElapsedTime();
- if(autoRotate && !dragging){
- camTheta += dt*0.12;
- updateCamera();
- }
- ringA.lookAt(camera.position);
- ringB.lookAt(camera.position);
- if(cometMesh && pathA && pathB && showPath){
- if(pathPlaying){
- pathProgress += dt*0.22*pathDirection;
- if(pathProgress>=1){ pathProgress=1; pathDirection=-1; }
- if(pathProgress<=0){ pathProgress=0; pathDirection=1; }
- pathScrubEl.value = (pathProgress*100).toFixed(1);
- }
- const sample = samplePath(pathA, pathB, pathProgress, pathType);
- cometMesh.visible = true;
- cometMesh.position.copy(sample.pos);
- cometMesh.material.color.setRGB(sample.rgb[0], sample.rgb[1], sample.rgb[2]);
- const s = 1 + Math.sin(t*6)*0.12;
- cometMesh.scale.set(s,s,s);
- swatchPathEl.style.background = rgbToHex(sample.rgb);
- notationPathEl.textContent = formatNotationFromAngle(sample.hue, sample.value, sample.chroma);
- } else if(cometMesh){
- cometMesh.visible = false;
- }
- renderer.render(scene, camera);
- }
- animate();
- })();
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment