Guest User

Untitled

a guest
Jul 4th, 2026
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 34.96 KB | Source Code | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Munsell Color Solid</title>
  6. <style>
  7.   @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');
  8.  
  9.   :root{
  10.     --bg: #0c0c10;
  11.     --panel: rgba(20,20,26,0.82);
  12.     --panel-border: #2c2c36;
  13.     --ink: #e9e6df;
  14.     --ink-dim: #9a978f;
  15.     --line: #33333e;
  16.     --accentA: #d0d0d0;
  17.     --accentB: #d0d0d0;
  18.   }
  19.   *{ box-sizing: border-box; }
  20.   html,body{
  21.     margin:0; padding:0; width:100%; height:100%;
  22.     background: var(--bg);
  23.     color: var(--ink);
  24.     font-family: 'Newsreader', serif;
  25.     overflow: hidden;
  26.   }
  27.   #scene{ position:absolute; inset:0; display:block; }
  28.  
  29.   .mono{ font-family:'IBM Plex Mono', monospace; letter-spacing:0.02em; }
  30.  
  31.   #title{
  32.     position:absolute; top:20px; left:24px; z-index:5;
  33.     pointer-events:none;
  34.   }
  35.   #title h1{
  36.     margin:0; font-size:22px; font-weight:500; font-style:italic;
  37.     color:var(--ink);
  38.   }
  39.   #title p{
  40.     margin:2px 0 0; font-size:11px; color:var(--ink-dim);
  41.     font-family:'IBM Plex Mono',monospace; letter-spacing:0.04em;
  42.     text-transform:uppercase;
  43.   }
  44.  
  45.   #controlsBar{
  46.     position:absolute; top:20px; right:24px; z-index:5;
  47.     display:flex; gap:8px; align-items:center;
  48.     flex-wrap:wrap; justify-content:flex-end;
  49.     max-width: 60vw;
  50.   }
  51.   .pill{
  52.     background: var(--panel);
  53.     border: 1px solid var(--panel-border);
  54.     color: var(--ink-dim);
  55.     font-family:'IBM Plex Mono',monospace;
  56.     font-size:11px;
  57.     padding:7px 12px;
  58.     border-radius: 3px;
  59.     cursor:pointer;
  60.     letter-spacing:0.03em;
  61.     transition: all .15s ease;
  62.     backdrop-filter: blur(6px);
  63.   }
  64.   .pill:hover{ color:var(--ink); border-color:#4a4a58; }
  65.   .pill.active{ color:#0c0c10; background: var(--ink); border-color: var(--ink); }
  66.   .pill.mini{ padding:5px 9px; font-size:10px; }
  67.   .barDivider{ width:1px; height:18px; background: var(--panel-border); margin:0 2px; }
  68.   .sizeCtl{ display:flex; align-items:center; gap:7px; }
  69.   .sizeCtl span{ font-size:10px; color:var(--ink-dim); white-space:nowrap; }
  70.   .sizeCtl input[type=range]{ width:64px; }
  71.  
  72.   .panel{
  73.     position:absolute; bottom:24px; z-index:5;
  74.     width: 300px;
  75.     background: var(--panel);
  76.     border: 1px solid var(--panel-border);
  77.     border-radius: 4px;
  78.     padding: 16px 18px 18px;
  79.     backdrop-filter: blur(8px);
  80.   }
  81.   #panelA{ left:24px; }
  82.   #panelB{ right:24px; }
  83.  
  84.   .panel-head{
  85.     display:flex; align-items:center; gap:10px; margin-bottom:12px;
  86.   }
  87.   .swatch{
  88.     width:38px; height:38px; border-radius:3px;
  89.     border:1px solid rgba(255,255,255,0.15);
  90.     box-shadow: inset 0 0 12px rgba(0,0,0,0.25);
  91.     flex-shrink:0;
  92.   }
  93.   .panel-head .label{
  94.     font-family:'IBM Plex Mono',monospace;
  95.     font-size:10px; color:var(--ink-dim); text-transform:uppercase; letter-spacing:0.08em;
  96.   }
  97.   .panel-head .notation{
  98.     font-family:'IBM Plex Mono',monospace;
  99.     font-size:15px; color:var(--ink); font-weight:500;
  100.   }
  101.  
  102.   .row{ margin-bottom:11px; }
  103.   .row:last-child{ margin-bottom:0; }
  104.   .row-top{
  105.     display:flex; justify-content:space-between;
  106.     font-family:'IBM Plex Mono',monospace;
  107.     font-size:10px; color:var(--ink-dim); margin-bottom:4px;
  108.     text-transform:uppercase; letter-spacing:0.06em;
  109.   }
  110.   .row-top span.val{ color: var(--ink); }
  111.  
  112.   input[type=range]{
  113.     -webkit-appearance:none; width:100%; height:2px;
  114.     background: var(--line); border-radius:2px; outline:none;
  115.     margin:0;
  116.   }
  117.   input[type=range]::-webkit-slider-thumb{
  118.     -webkit-appearance:none; width:12px; height:12px; border-radius:50%;
  119.     background: var(--ink); cursor:pointer;
  120.     border: 2px solid var(--bg);
  121.     box-shadow: 0 0 0 1px var(--ink);
  122.   }
  123.   input[type=range]::-moz-range-thumb{
  124.     width:12px; height:12px; border-radius:50%;
  125.     background: var(--ink); cursor:pointer; border: 2px solid var(--bg);
  126.   }
  127.  
  128.   #pathInfo{
  129.     position:absolute; bottom:24px; left:50%; transform:translateX(-50%);
  130.     z-index:5; text-align:center;
  131.     background: var(--panel);
  132.     border: 1px solid var(--panel-border);
  133.     border-radius: 4px;
  134.     padding: 10px 22px;
  135.     font-family:'IBM Plex Mono',monospace;
  136.     backdrop-filter: blur(8px);
  137.     display:none;
  138.   }
  139.   #pathInfo .d{ font-size:10px; color:var(--ink-dim); text-transform:uppercase; letter-spacing:0.06em; margin-top:8px; }
  140.   #pathInfo .v{ font-size:14px; color:var(--ink); margin-top:2px; }
  141.   #pathTypeGroup{ display:flex; gap:6px; justify-content:center; }
  142.   .scrubRow{ display:flex; align-items:center; gap:9px; margin-top:10px; }
  143.   .scrubRow input[type=range]{ flex:1; }
  144.   #onPathHead{ margin-top:12px; margin-bottom:0; padding-top:10px; border-top:1px solid var(--line); justify-content:center; }
  145.  
  146.   #hint{
  147.     position:absolute; top:64px; left:24px; z-index:4;
  148.     font-family:'IBM Plex Mono',monospace; font-size:10px; color:var(--ink-dim);
  149.     letter-spacing:0.03em; opacity:0.7;
  150.   }
  151.  
  152.   @media (max-width: 820px){
  153.     .panel{ width: calc(50% - 32px); padding:12px 12px 14px; }
  154.     #title h1{ font-size:17px; }
  155.     #hint{ display:none; }
  156.     .row-top{ font-size:9px; }
  157.   }
  158. </style>
  159. </head>
  160. <body>
  161.  
  162. <canvas id="scene"></canvas>
  163.  
  164. <div id="title">
  165.   <h1>The Munsell Color Solid</h1>
  166.   <p>Hue &middot; Value &middot; Chroma &mdash; an approximate perceptual rendering</p>
  167. </div>
  168.  
  169. <div id="hint">drag to orbit &middot; scroll to zoom</div>
  170.  
  171. <div id="controlsBar">
  172.   <button class="pill" id="btnSolid">solid</button>
  173.   <button class="pill" id="btnWire">wire</button>
  174.   <button class="pill active" id="btnPoints">points</button>
  175.   <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>
  176.   <div class="barDivider"></div>
  177.   <button class="pill" id="btnRotate">auto-rotate</button>
  178.   <button class="pill active" id="btnPath">trace path</button>
  179. </div>
  180.  
  181. <div class="panel" id="panelA">
  182.   <div class="panel-head">
  183.     <div class="swatch" id="swatchA"></div>
  184.     <div>
  185.       <div class="label">Color A</div>
  186.       <div class="notation" id="notationA">5.0R 5.0/10.0</div>
  187.     </div>
  188.   </div>
  189.   <div class="row">
  190.     <div class="row-top"><span>Hue</span><span class="val" id="hueLabelA">5.0R</span></div>
  191.     <input type="range" id="hueA" min="0" max="100" step="0.1" value="5">
  192.   </div>
  193.   <div class="row">
  194.     <div class="row-top"><span>Value</span><span class="val" id="valLabelA">5.0</span></div>
  195.     <input type="range" id="valA" min="0" max="10" step="0.1" value="5">
  196.   </div>
  197.   <div class="row">
  198.     <div class="row-top"><span>Chroma</span><span class="val" id="chrLabelA">10.0</span></div>
  199.     <input type="range" id="chrA" min="0" max="20" step="0.1" value="10">
  200.   </div>
  201. </div>
  202.  
  203. <div class="panel" id="panelB">
  204.   <div class="panel-head">
  205.     <div class="swatch" id="swatchB"></div>
  206.     <div>
  207.       <div class="label">Color B</div>
  208.       <div class="notation" id="notationB">75.0PB 4.0/10.0</div>
  209.     </div>
  210.   </div>
  211.   <div class="row">
  212.     <div class="row-top"><span>Hue</span><span class="val" id="hueLabelB">75.0PB</span></div>
  213.     <input type="range" id="hueB" min="0" max="100" step="0.1" value="75">
  214.   </div>
  215.   <div class="row">
  216.     <div class="row-top"><span>Value</span><span class="val" id="valLabelB">4.0</span></div>
  217.     <input type="range" id="valB" min="0" max="10" step="0.1" value="4">
  218.   </div>
  219.   <div class="row">
  220.     <div class="row-top"><span>Chroma</span><span class="val" id="chrLabelB">10.0</span></div>
  221.     <input type="range" id="chrB" min="0" max="20" step="0.1" value="10">
  222.   </div>
  223. </div>
  224.  
  225. <div id="pathInfo">
  226.   <div id="pathTypeGroup">
  227.     <button class="pill mini active" data-type="munsell">Munsell</button>
  228.     <button class="pill mini" data-type="paint">Paint mix</button>
  229.     <button class="pill mini" data-type="digital">Digital RGB</button>
  230.     <button class="pill mini" data-type="linear">Linear</button>
  231.   </div>
  232.   <div class="d" id="pathCaption">Interpolated directly through hue, value &amp; chroma</div>
  233.   <div class="v" id="pathDist">&mdash;</div>
  234.   <div class="scrubRow">
  235.     <button class="pill mini active" id="btnPlayPause">pause</button>
  236.     <input type="range" id="pathScrub" min="0" max="100" step="0.5" value="0">
  237.   </div>
  238.   <div class="panel-head" id="onPathHead">
  239.     <div class="swatch" id="swatchPath"></div>
  240.     <div>
  241.       <div class="label">On path</div>
  242.       <div class="notation" id="notationPath">&mdash;</div>
  243.     </div>
  244.   </div>
  245. </div>
  246.  
  247. <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
  248. <script>
  249. (function(){
  250.  
  251. /* ---------------------------------------------------------------
  252.    MUNSELL APPROXIMATION MODEL
  253.    Ten principal hue families, evenly spaced 36° apart:
  254.    R, YR, Y, GY, G, BG, B, PB, P, RP
  255.    Each family has an approximate value at which chroma peaks,
  256.    and an approximate maximum chroma at that peak. This produces
  257.    the characteristic irregular "potato" shaped color solid rather
  258.    than a uniform cylinder or sphere.
  259. ------------------------------------------------------------------*/
  260.  
  261. const FAMILIES = ['R','YR','Y','GY','G','BG','B','PB','P','RP'];
  262. // peak value (0-10) and peak chroma (relative) per family
  263. const PEAK_VALUE  = [4.5, 6.0, 8.2, 7.2, 5.2, 5.0, 4.2, 3.6, 3.6, 4.2];
  264. const PEAK_CHROMA = [15,  12,  13,  10,  8.5, 7.0, 8.5, 13,  11,  14 ];
  265. // perceptual hue-wheel degree that each family roughly corresponds to,
  266. // used only for coloring (Munsell hue spacing is NOT perceptually uniform
  267. // in RGB terms, so this remaps the 0-360 munsell angle to an approximate
  268. // display hue angle)
  269. const DISPLAY_DEG = [0, 28, 58, 95, 132, 168, 210, 255, 288, 325, 360];
  270.  
  271. function wrapIndex(i){ return ((i % 10) + 10) % 10; }
  272.  
  273. // Given a continuous munsell angle 0-360, interpolate peakValue/peakChroma
  274. function familyInterp(angleDeg, arr){
  275.   const pos = ((angleDeg % 360) + 360) % 360 / 36; // 0-10 continuous
  276.   const i0 = Math.floor(pos);
  277.   const t = pos - i0;
  278.   const i1 = wrapIndex(i0 + 1);
  279.   const a = arr[wrapIndex(i0)];
  280.   const b = arr[i1];
  281.   // smoothstep interpolation for a gentler blend between families
  282.   const s = t*t*(3-2*t);
  283.   return a + (b-a)*s;
  284. }
  285.  
  286. function maxChromaAt(angleDeg, value){
  287.   const peakV = familyInterp(angleDeg, PEAK_VALUE);
  288.   const peakC = familyInterp(angleDeg, PEAK_CHROMA);
  289.   const sigma = 3.1;
  290.   const bell = Math.exp(-((value-peakV)*(value-peakV))/(2*sigma*sigma));
  291.   const envelope = Math.sin(Math.PI * Math.min(Math.max(value,0),10) / 10); // ->0 at V=0 and V=10
  292.   return Math.max(0, peakC * bell * envelope);
  293. }
  294.  
  295. function displayHueDeg(angleDeg){
  296.   const pos = ((angleDeg % 360) + 360) % 360 / 36;
  297.   const i0 = Math.floor(pos);
  298.   const t = pos - i0;
  299.   const a = DISPLAY_DEG[i0];
  300.   const b = DISPLAY_DEG[i0+1];
  301.   return a + (b-a)*t;
  302. }
  303.  
  304. // inverse of displayHueDeg: given an RGB-wheel hue degree, recover the
  305. // approximate munsell angle (used to plot RGB-space interpolations back
  306. // inside the munsell coordinate system)
  307. function inverseDisplayHueDeg(displayDeg){
  308.   const d = ((displayDeg%360)+360)%360;
  309.   for(let i=0;i<10;i++){
  310.    const a = DISPLAY_DEG[i], b = DISPLAY_DEG[i+1];
  311.    if(d>=a && d<=b){
  312.      const t = (b-a)>0 ? (d-a)/(b-a) : 0;
  313.       return i*36 + t*36;
  314.     }
  315.   }
  316.   return d;
  317. }
  318.  
  319. function rgbToHsl(r,g,b){
  320.   const max = Math.max(r,g,b), min = Math.min(r,g,b);
  321.   let h,s;
  322.   const l = (max+min)/2;
  323.   if(max===min){ h=0; s=0; }
  324.   else{
  325.     const d = max-min;
  326.     s = l>0.5 ? d/(2-max-min) : d/(max+min);
  327.     switch(max){
  328.       case r: h = (g-b)/d + (g<b?6:0); break;
  329.      case g: h = (b-r)/d + 2; break;
  330.      default: h = (r-g)/d + 4;
  331.    }
  332.    h *= 60;
  333.  }
  334.  return [h,s,l];
  335. }
  336.  
  337. function hslToRgb(h,s,l){
  338.  h = ((h%360)+360)%360/360;
  339.  let r,g,b;
  340.  if(s===0){ r=g=b=l; }
  341.  else{
  342.    const hue2rgb=(p,q,t)=>{
  343.       if(t<0) t+=1; if(t>1) t-=1;
  344.       if(t<1/6) return p+(q-p)*6*t;
  345.      if(t<1/2) return q;
  346.      if(t<2/3) return p+(q-p)*(2/3-t)*6;
  347.      return p;
  348.    };
  349.    const q = l<0.5 ? l*(1+s) : l+s-l*s;
  350.    const p = 2*l-q;
  351.    r = hue2rgb(p,q,h+1/3);
  352.    g = hue2rgb(p,q,h);
  353.    b = hue2rgb(p,q,h-1/3);
  354.  }
  355.  return [r,g,b];
  356. }
  357.  
  358. // hue: 0-360 munsell angle, value: 0-10, chroma: 0-max
  359. function munsellToRgb(hueDeg, value, chroma){
  360.  const maxC = maxChromaAt(hueDeg, value);
  361.  const sat = maxC > 0.0001 ? Math.min(chroma/maxC, 1) * 0.92 : 0;
  362.   const l = Math.min(Math.max(value/10, 0), 1);
  363.   const dh = displayHueDeg(hueDeg);
  364.   return hslToRgb(dh, sat, l);
  365. }
  366.  
  367. function rgbToHex(rgb){
  368.   const c = rgb.map(v=>Math.round(Math.min(Math.max(v,0),1)*255));
  369.   return '#'+c.map(v=>v.toString(16).padStart(2,'0')).join('');
  370. }
  371.  
  372. // Convert (hueDeg 0-360, value 0-10, chroma 0-max) -> cartesian scene coords
  373. function munsellToXYZ(hueDeg, value, chroma){
  374.   const rad = hueDeg * Math.PI/180;
  375.   const scale = 0.34; // chroma units -> scene units
  376.   const x = Math.cos(rad) * chroma * scale;
  377.   const z = Math.sin(rad) * chroma * scale;
  378.   const y = (value - 5) * 0.85;
  379.   return new THREE.Vector3(x,y,z);
  380. }
  381.  
  382. function xyzToMunsell(pos){
  383.   const scale = 0.34;
  384.   const value = pos.y/0.85 + 5;
  385.   const radius = Math.sqrt(pos.x*pos.x + pos.z*pos.z);
  386.   const chroma = radius/scale;
  387.   let hue = Math.atan2(pos.z, pos.x) * 180/Math.PI;
  388.   if(hue<0) hue += 360;
  389.  return {hue, value, chroma};
  390. }
  391.  
  392. // hue slider (0-100) -> munsell angle (0-360) and notation label
  393. function hueSliderToDeg(v){ return (v/100)*360; }
  394. function formatNotationFromAngle(angleDeg, value, chroma){
  395.   const famPos = ((angleDeg%360)+360)%360/36;
  396.   const fi = Math.floor(famPos+1e-9) % 10;
  397.   const sub = (famPos - fi) * 10;
  398.   return `${sub.toFixed(1)}${FAMILIES[fi]} ${value.toFixed(1)}/${chroma.toFixed(1)}`;
  399. }
  400. function notationLabel(hueSlider, value, chroma){
  401.   return formatNotationFromAngle(hueSliderToDeg(hueSlider), value, chroma);
  402. }
  403.  
  404. /* ---------------------------------------------------------------
  405.    THREE.JS SCENE SETUP
  406. ------------------------------------------------------------------*/
  407. const canvas = document.getElementById('scene');
  408. const renderer = new THREE.WebGLRenderer({canvas, antialias:true, alpha:false});
  409. renderer.setPixelRatio(Math.min(window.devicePixelRatio,2));
  410. const scene = new THREE.Scene();
  411. scene.background = new THREE.Color(0x0c0c10);
  412. scene.fog = new THREE.Fog(0x0c0c10, 14, 30);
  413.  
  414. const camera = new THREE.PerspectiveCamera(42, window.innerWidth/window.innerHeight, 0.1, 100);
  415.  
  416. // simple orbit control implemented manually
  417. let camTheta = Math.PI*0.28, camPhi = Math.PI*0.38, camRadius = 11;
  418. const camTarget = new THREE.Vector3(0,0,0);
  419. function updateCamera(){
  420.   const p = Math.min(Math.max(camPhi, 0.12), Math.PI-0.12);
  421.   camera.position.set(
  422.     camTarget.x + camRadius*Math.sin(p)*Math.sin(camTheta),
  423.     camTarget.y + camRadius*Math.cos(p),
  424.     camTarget.z + camRadius*Math.sin(p)*Math.cos(camTheta)
  425.   );
  426.   camera.lookAt(camTarget);
  427. }
  428. updateCamera();
  429.  
  430. let dragging=false, lastX=0, lastY=0;
  431. canvas.addEventListener('pointerdown', e=>{ dragging=true; lastX=e.clientX; lastY=e.clientY; autoRotate=false; setRotateBtn(); });
  432. window.addEventListener('pointerup', ()=> dragging=false);
  433. window.addEventListener('pointermove', e=>{
  434.   if(!dragging) return;
  435.   const dx = e.clientX-lastX, dy=e.clientY-lastY;
  436.   lastX=e.clientX; lastY=e.clientY;
  437.   camTheta -= dx*0.0055;
  438.   camPhi   -= dy*0.0055;
  439.   updateCamera();
  440. });
  441. canvas.addEventListener('wheel', e=>{
  442.   e.preventDefault();
  443.   camRadius = Math.min(Math.max(camRadius + e.deltaY*0.01, 4), 24);
  444.   updateCamera();
  445. }, {passive:false});
  446.  
  447. // touch pinch / drag support (basic)
  448. let lastTouchDist=null;
  449. canvas.addEventListener('touchstart', e=>{
  450.   if(e.touches.length===1){ dragging=true; lastX=e.touches[0].clientX; lastY=e.touches[0].clientY; autoRotate=false; setRotateBtn(); }
  451.   else if(e.touches.length===2){
  452.     lastTouchDist = Math.hypot(e.touches[0].clientX-e.touches[1].clientX, e.touches[0].clientY-e.touches[1].clientY);
  453.   }
  454. }, {passive:true});
  455. canvas.addEventListener('touchmove', e=>{
  456.   if(e.touches.length===1 && dragging){
  457.    const dx=e.touches[0].clientX-lastX, dy=e.touches[0].clientY-lastY;
  458.     lastX=e.touches[0].clientX; lastY=e.touches[0].clientY;
  459.     camTheta -= dx*0.006; camPhi -= dy*0.006;
  460.     updateCamera();
  461.   } else if(e.touches.length===2 && lastTouchDist){
  462.    const d = Math.hypot(e.touches[0].clientX-e.touches[1].clientX, e.touches[0].clientY-e.touches[1].clientY);
  463.     camRadius = Math.min(Math.max(camRadius + (lastTouchDist-d)*0.02, 4), 24);
  464.     lastTouchDist = d;
  465.     updateCamera();
  466.   }
  467. }, {passive:true});
  468. canvas.addEventListener('touchend', ()=>{ dragging=false; lastTouchDist=null; });
  469.  
  470. function resize(){
  471.   const w = window.innerWidth, h = window.innerHeight;
  472.   renderer.setSize(w,h);
  473.   camera.aspect = w/h;
  474.   camera.updateProjectionMatrix();
  475. }
  476. window.addEventListener('resize', resize);
  477. resize();
  478.  
  479. // lighting
  480. scene.add(new THREE.AmbientLight(0xffffff, 0.55));
  481. const key = new THREE.DirectionalLight(0xffffff, 0.9);
  482. key.position.set(6,10,8);
  483. scene.add(key);
  484. const fill = new THREE.DirectionalLight(0x8899ff, 0.25);
  485. fill.position.set(-8,-4,-6);
  486. scene.add(fill);
  487.  
  488. /* ---------------------------------------------------------------
  489.    BUILD THE COLOR SOLID (outer chroma envelope surface)
  490. ------------------------------------------------------------------*/
  491. const VALUE_STEPS = 48;
  492. const HUE_STEPS = 64;
  493.  
  494. function buildSolidGeometry(){
  495.   const positions = [];
  496.   const colors = [];
  497.   const indices = [];
  498.   const grid = [];
  499.  
  500.   for(let vi=0; vi<=VALUE_STEPS; vi++){
  501.    const value = (vi/VALUE_STEPS)*10;
  502.    const row = [];
  503.    for(let hi=0; hi<=HUE_STEPS; hi++){
  504.      const angle = (hi/HUE_STEPS)*360;
  505.      const chroma = maxChromaAt(angle, value);
  506.      const pos = munsellToXYZ(angle, value, chroma);
  507.      const rgb = munsellToRgb(angle, value, chroma);
  508.      positions.push(pos.x, pos.y, pos.z);
  509.      colors.push(rgb[0], rgb[1], rgb[2]);
  510.      row.push(positions.length/3 - 1);
  511.    }
  512.    grid.push(row);
  513.  }
  514.  for(let vi=0; vi<VALUE_STEPS; vi++){
  515.    for(let hi=0; hi<HUE_STEPS; hi++){
  516.      const a = grid[vi][hi], b = grid[vi][hi+1];
  517.      const c = grid[vi+1][hi], d = grid[vi+1][hi+1];
  518.      indices.push(a,c,b,  b,c,d);
  519.    }
  520.  }
  521.  const geo = new THREE.BufferGeometry();
  522.  geo.setAttribute('position', new THREE.Float32BufferAttribute(positions,3));
  523.  geo.setAttribute('color', new THREE.Float32BufferAttribute(colors,3));
  524.  geo.setIndex(indices);
  525.  geo.computeVertexNormals();
  526.  return geo;
  527. }
  528.  
  529. const solidGeo = buildSolidGeometry();
  530. const solidMat = new THREE.MeshPhongMaterial({
  531.  vertexColors:true, shininess:8, specular:0x222222,
  532.  side:THREE.DoubleSide
  533. });
  534. const solidMesh = new THREE.Mesh(solidGeo, solidMat);
  535. solidMesh.visible = false;
  536. scene.add(solidMesh);
  537.  
  538. // wireframe overlay (meridians + parallels), hidden by default
  539. const wireGeo = new THREE.EdgesGeometry(solidGeo, 1);
  540. const wireMat = new THREE.LineBasicMaterial({color:0x000000, transparent:true, opacity:0.18});
  541. const wireMesh = new THREE.LineSegments(wireGeo, wireMat);
  542. wireMesh.visible = false;
  543. scene.add(wireMesh);
  544.  
  545. // points mode: a volumetric fill (not just the outer shell) so the
  546. // desaturated colors living inside the solid are visible too.
  547. function buildPointCloudGeometry(){
  548.  const HUE_N = 40;
  549.  const VALUE_N = 27;
  550.  const CHROMA_N = 9; // radial samples from center out to the max-chroma shell
  551.  
  552.  const positions = [];
  553.  const colors = [];
  554.  
  555.  for(let vi=0; vi<=VALUE_N; vi++){
  556.    const value = (vi/VALUE_N)*10;
  557.    for(let hi=0; hi<HUE_N; hi++){
  558.      const angle = (hi/HUE_N)*360;
  559.      const maxC = maxChromaAt(angle, value);
  560.      for(let ci=0; ci<=CHROMA_N; ci++){
  561.        // skip repeating the center point for every hue slice
  562.        if(ci===0 && hi>0) continue;
  563.         const frac = ci/CHROMA_N;
  564.         const chroma = frac*maxC;
  565.         const pos = munsellToXYZ(angle, value, chroma);
  566.         const rgb = munsellToRgb(angle, value, chroma);
  567.         positions.push(pos.x,pos.y,pos.z);
  568.         colors.push(rgb[0],rgb[1],rgb[2]);
  569.       }
  570.     }
  571.   }
  572.   const geo = new THREE.BufferGeometry();
  573.   geo.setAttribute('position', new THREE.Float32BufferAttribute(positions,3));
  574.   geo.setAttribute('color', new THREE.Float32BufferAttribute(colors,3));
  575.   return geo;
  576. }
  577. const cloudGeo = buildPointCloudGeometry();
  578. const pointsMat = new THREE.PointsMaterial({size:0.13, vertexColors:true, sizeAttenuation:true, transparent:true, opacity:0.92});
  579. const pointsMesh = new THREE.Points(cloudGeo, pointsMat);
  580. pointsMesh.visible = true;
  581. scene.add(pointsMesh);
  582.  
  583. document.getElementById('ptSize').addEventListener('input', function(){
  584.   pointsMat.size = parseFloat(this.value);
  585. });
  586.  
  587. /* central neutral axis: black at bottom, white at top */
  588. (function(){
  589.   const segs = 32;
  590.   const pos = [], col = [];
  591.   for(let i=0;i<=segs;i++){
  592.    const v = (i/segs)*10;
  593.    pos.push(0, (v-5)*0.85, 0);
  594.    const l = v/10;
  595.    col.push(l,l,l);
  596.  }
  597.  const geo = new THREE.BufferGeometry();
  598.  geo.setAttribute('position', new THREE.Float32BufferAttribute(pos,3));
  599.  geo.setAttribute('color', new THREE.Float32BufferAttribute(col,3));
  600.  const mat = new THREE.LineBasicMaterial({vertexColors:true, linewidth:2});
  601.  scene.add(new THREE.Line(geo, mat));
  602.  // small ticks
  603.  for(let v=0; v<=10; v+=2){
  604.    const l = v/10;
  605.    const tickGeo = new THREE.SphereGeometry(0.035, 10, 10);
  606.    const tickMat = new THREE.MeshBasicMaterial({color: new THREE.Color(l,l,l)});
  607.    const tick = new THREE.Mesh(tickGeo, tickMat);
  608.    tick.position.set(0,(v-5)*0.85,0);
  609.    scene.add(tick);
  610.  }
  611. })();
  612.  
  613. /* text sprite helper */
  614. function makeTextSprite(text, opts={}){
  615.  const fontSize = opts.fontSize || 64;
  616.  const color = opts.color || '#e9e6df';
  617.  const canvasEl = document.createElement('canvas');
  618.  const ctx = canvasEl.getContext('2d');
  619.  ctx.font = `500 ${fontSize}px 'IBM Plex Mono', monospace`;
  620.  const metrics = ctx.measureText(text);
  621.  const pad = fontSize*0.4;
  622.  canvasEl.width = metrics.width + pad*2;
  623.  canvasEl.height = fontSize*1.5;
  624.  ctx.font = `500 ${fontSize}px 'IBM Plex Mono', monospace`;
  625.  ctx.fillStyle = color;
  626.  ctx.textBaseline = 'middle';
  627.  ctx.fillText(text, pad, canvasEl.height/2);
  628.  const tex = new THREE.CanvasTexture(canvasEl);
  629.  tex.needsUpdate = true;
  630.  const mat = new THREE.SpriteMaterial({map:tex, transparent:true, depthTest:false});
  631.  const sprite = new THREE.Sprite(mat);
  632.  const scale = (opts.scale || 0.5);
  633.  sprite.scale.set(scale * canvasEl.width/canvasEl.height, scale, 1);
  634.  sprite.renderOrder = 999;
  635.  return sprite;
  636. }
  637.  
  638. // hue family labels around the equator (value = 5 ring, just outside max chroma)
  639. FAMILIES.forEach((name, i)=>{
  640.   const angle = i*36;
  641.   const value = 5;
  642.   const chroma = maxChromaAt(angle, value) + 2.4;
  643.   const p = munsellToXYZ(angle, value, chroma);
  644.   const label = makeTextSprite(name, {fontSize:56, color:'#c9c6bd', scale:0.42});
  645.   label.position.copy(p);
  646.   scene.add(label);
  647. });
  648.  
  649. // value labels along the axis
  650. [0,2,4,6,8,10].forEach(v=>{
  651.   const label = makeTextSprite('V'+v, {fontSize:48, color:'#7d7a72', scale:0.3});
  652.   label.position.set(0.55, (v-5)*0.85, 0);
  653.   scene.add(label);
  654. });
  655.  
  656. /* ---------------------------------------------------------------
  657.    COLOR A / COLOR B markers + path
  658. ------------------------------------------------------------------*/
  659. const markerAGeo = new THREE.SphereGeometry(0.14, 24, 24);
  660. const markerBGeo = new THREE.SphereGeometry(0.14, 24, 24);
  661. const markerAMat = new THREE.MeshBasicMaterial({color:0xffffff});
  662. const markerBMat = new THREE.MeshBasicMaterial({color:0xffffff});
  663. const markerA = new THREE.Mesh(markerAGeo, markerAMat);
  664. const markerB = new THREE.Mesh(markerBGeo, markerBMat);
  665. const ringGeoA = new THREE.RingGeometry(0.18,0.22,32);
  666. const ringGeoB = new THREE.RingGeometry(0.18,0.22,32);
  667. const ringMatA = new THREE.MeshBasicMaterial({color:0xffffff, side:THREE.DoubleSide, transparent:true, opacity:0.85});
  668. const ringMatB = new THREE.MeshBasicMaterial({color:0xffffff, side:THREE.DoubleSide, transparent:true, opacity:0.85});
  669. const ringA = new THREE.Mesh(ringGeoA, ringMatA);
  670. const ringB = new THREE.Mesh(ringGeoB, ringMatB);
  671. scene.add(markerA, markerB, ringA, ringB);
  672.  
  673. let pathGroup = new THREE.Group();
  674. scene.add(pathGroup);
  675. let cometMesh = null;
  676. let cometCurvePoints = [];
  677. let pathA = null, pathB = null;
  678. let pathProgress = 0, pathDirection = 1, pathPlaying = true;
  679.  
  680. function shortestAngleLerp(a,b,t){
  681.   let diff = ((b-a+540)%360)-180;
  682.   return a + diff*t;
  683. }
  684.  
  685. const PATH_CAPTIONS = {
  686.   munsell: 'Interpolated directly through hue, value &amp; chroma',
  687.   paint: 'Dips toward muddy gray, as complementary pigments do when physically mixed',
  688.   digital: 'Straight blend of RGB pixel values, as a gradient tool would draw it',
  689.   linear: 'A straight line through the 3-D space itself &mdash; true complements cross the gray axis'
  690. };
  691.  
  692. function angularDist(a,b){
  693.   return Math.abs(((b-a+540)%360)-180);
  694. }
  695.  
  696. // sample point + color for a given interpolation type at parameter t
  697. function samplePath(A,B,t,type){
  698.   if(type==='munsell'){
  699.     const h = shortestAngleLerp(A.hue, B.hue, t);
  700.     const v = A.value + (B.value-A.value)*t;
  701.     const c = A.chroma + (B.chroma-A.chroma)*t;
  702.     return {pos: munsellToXYZ(h,v,c), rgb: munsellToRgb(h,v,c), hue:h, value:v, chroma:c};
  703.   }
  704.   if(type==='paint'){
  705.     // real pigments mixed together tend to desaturate toward a muddy
  706.     // neutral in the middle, more so the further apart the hues are.
  707.     const ang = angularDist(A.hue,B.hue);
  708.     const dip = 0.15 + 0.65*(ang/180);
  709.     const bell = Math.sin(Math.PI*t);
  710.     const h = shortestAngleLerp(A.hue, B.hue, t);
  711.     let v = A.value + (B.value-A.value)*t;
  712.     v -= 0.9*bell*(ang/180); // mixtures often run slightly darker/duller
  713.     v = Math.min(Math.max(v,0),10);
  714.     const cBase = A.chroma + (B.chroma-A.chroma)*t;
  715.     const c = Math.max(cBase*(1-dip*bell), 0);
  716.     return {pos: munsellToXYZ(h,v,c), rgb: munsellToRgb(h,v,c), hue:h, value:v, chroma:c};
  717.   }
  718.   if(type==='linear'){
  719.     const posA = munsellToXYZ(A.hue, A.value, A.chroma);
  720.     const posB = munsellToXYZ(B.hue, B.value, B.chroma);
  721.     const pos = posA.clone().lerp(posB, t);
  722.     const m = xyzToMunsell(pos);
  723.     return {pos, rgb: munsellToRgb(m.hue, m.value, m.chroma), hue:m.hue, value:m.value, chroma:m.chroma};
  724.   }
  725.   // digital: linear blend in RGB space (as a screen gradient tool would do),
  726.   // then approximately re-plotted back into munsell coordinates
  727.   const rgbA = munsellToRgb(A.hue,A.value,A.chroma);
  728.   const rgbB = munsellToRgb(B.hue,B.value,B.chroma);
  729.   const r = rgbA[0]+(rgbB[0]-rgbA[0])*t;
  730.   const g = rgbA[1]+(rgbB[1]-rgbA[1])*t;
  731.   const b = rgbA[2]+(rgbB[2]-rgbA[2])*t;
  732.   const [hh,ss,ll] = rgbToHsl(r,g,b);
  733.   const munsellAngle = inverseDisplayHueDeg(hh);
  734.   const value = ll*10;
  735.   const maxC = maxChromaAt(munsellAngle, value);
  736.   const chroma = Math.min((ss/0.92)*maxC, maxC);
  737.   return {pos: munsellToXYZ(munsellAngle, value, chroma), rgb:[r,g,b], hue:munsellAngle, value:value, chroma:chroma};
  738. }
  739.  
  740. function rebuildPath(A,B, visible){
  741.   pathGroup.clear();
  742.   if(!visible) {
  743.     document.getElementById('pathInfo').style.display='none';
  744.     pathA = null; pathB = null;
  745.     return;
  746.   }
  747.   document.getElementById('pathInfo').style.display='block';
  748.   document.getElementById('pathCaption').innerHTML = PATH_CAPTIONS[pathType];
  749.   pathA = A; pathB = B;
  750.  
  751.   const N = 60;
  752.   const pts = [];
  753.   const cols = [];
  754.   for(let i=0;i<=N;i++){
  755.    const t = i/N;
  756.    const sample = samplePath(A,B,t,pathType);
  757.    pts.push(sample.pos);
  758.    cols.push(sample.rgb);
  759.  }
  760.  cometCurvePoints = pts;
  761.  
  762.  // line with vertex colors
  763.  const positions = [];
  764.  const colorsArr = [];
  765.  pts.forEach((p,i)=>{
  766.     positions.push(p.x,p.y,p.z);
  767.     colorsArr.push(cols[i][0],cols[i][1],cols[i][2]);
  768.   });
  769.   const lineGeo = new THREE.BufferGeometry();
  770.   lineGeo.setAttribute('position', new THREE.Float32BufferAttribute(positions,3));
  771.   lineGeo.setAttribute('color', new THREE.Float32BufferAttribute(colorsArr,3));
  772.   const lineMat = new THREE.LineBasicMaterial({vertexColors:true, linewidth:2});
  773.   pathGroup.add(new THREE.Line(lineGeo, lineMat));
  774.  
  775.   // small bead markers along path for thickness illusion
  776.   for(let i=0;i<=N;i+=4){
  777.    const beadGeo = new THREE.SphereGeometry(0.045, 10, 10);
  778.    const rgb = cols[i];
  779.    const beadMat = new THREE.MeshBasicMaterial({color: new THREE.Color(rgb[0],rgb[1],rgb[2])});
  780.    const bead = new THREE.Mesh(beadGeo, beadMat);
  781.    bead.position.copy(pts[i]);
  782.    pathGroup.add(bead);
  783.  }
  784.  
  785.  // traveling comet — its color is set every frame to match the color
  786.  // it currently sits on (see animate())
  787.  const cometGeo = new THREE.SphereGeometry(0.1, 20, 20);
  788.  const cometMat = new THREE.MeshBasicMaterial({color:0xffffff});
  789.  cometMesh = new THREE.Mesh(cometGeo, cometMat);
  790.  pathGroup.add(cometMesh);
  791.  
  792.  // distance readout (perceptual-ish: cylindrical euclidean distance in scene units, rescaled)
  793.  const startP = pts[0], endP = pts[pts.length-1];
  794.  const dist = startP.distanceTo(endP) / 0.34; // back into chroma-ish units, rough
  795.  const straightDist = Math.sqrt(
  796.    Math.pow(A.value-B.value,2)*4 +
  797.    Math.pow(dist,2)
  798.  );
  799.  document.getElementById('pathDist').textContent = straightDist.toFixed(2) + ' units (\u0394V, \u0394H, \u0394C combined)';
  800. }
  801.  
  802. /* ---------------------------------------------------------------
  803.   UI WIRING
  804. ------------------------------------------------------------------*/
  805. function readColor(prefix){
  806.  const hueSlider = parseFloat(document.getElementById('hue'+prefix).value);
  807.  const value = parseFloat(document.getElementById('val'+prefix).value);
  808.  let chroma = parseFloat(document.getElementById('chr'+prefix).value);
  809.  const hue = hueSliderToDeg(hueSlider);
  810.  const maxC = maxChromaAt(hue, value);
  811.  chroma = Math.min(chroma, Math.max(maxC, 0.5));
  812.  return {hueSlider, hue, value, chroma, maxC};
  813. }
  814.  
  815. function famLabel(hueSlider){
  816.  const angle = hueSliderToDeg(hueSlider);
  817.  const famPos = ((angle%360)+360)%360/36;
  818.  const fi = Math.floor(famPos+1e-9) % 10;
  819.  const sub = (famPos - fi) * 10;
  820.  return sub.toFixed(1) + FAMILIES[fi];
  821. }
  822.  
  823. let showPath = true;
  824. let viewMode = 'points';
  825. let pathType = 'munsell';
  826. let autoRotate = false;
  827.  
  828. function refresh(){
  829.  const A = readColor('A');
  830.  const B = readColor('B');
  831.  
  832.  document.getElementById('chrA').max = Math.max(A.maxC, 1).toFixed(1);
  833.  document.getElementById('chrB').max = Math.max(B.maxC, 1).toFixed(1);
  834.  
  835.  document.getElementById('hueLabelA').textContent = famLabel(A.hueSlider);
  836.  document.getElementById('valLabelA').textContent = A.value.toFixed(1);
  837.  document.getElementById('chrLabelA').textContent = A.chroma.toFixed(1);
  838.  document.getElementById('hueLabelB').textContent = famLabel(B.hueSlider);
  839.  document.getElementById('valLabelB').textContent = B.value.toFixed(1);
  840.  document.getElementById('chrLabelB').textContent = B.chroma.toFixed(1);
  841.  
  842.  const rgbA = munsellToRgb(A.hue, A.value, A.chroma);
  843.  const rgbB = munsellToRgb(B.hue, B.value, B.chroma);
  844.  document.getElementById('swatchA').style.background = rgbToHex(rgbA);
  845.  document.getElementById('swatchB').style.background = rgbToHex(rgbB);
  846.  document.getElementById('notationA').textContent = notationLabel(A.hueSlider, A.value, A.chroma);
  847.  document.getElementById('notationB').textContent = notationLabel(B.hueSlider, B.value, B.chroma);
  848.  
  849.  const posA = munsellToXYZ(A.hue, A.value, A.chroma);
  850.  const posB = munsellToXYZ(B.hue, B.value, B.chroma);
  851.  markerA.position.copy(posA);
  852.  markerB.position.copy(posB);
  853.  ringA.position.copy(posA);
  854.  ringB.position.copy(posB);
  855.  markerAMat.color.setRGB(rgbA[0],rgbA[1],rgbA[2]);
  856.  markerBMat.color.setRGB(rgbB[0],rgbB[1],rgbB[2]);
  857.  
  858.  rebuildPath(A,B, showPath);
  859.  return {A,B};
  860. }
  861.  
  862. ['hueA','valA','chrA','hueB','valB','chrB'].forEach(id=>{
  863.   document.getElementById(id).addEventListener('input', refresh);
  864. });
  865.  
  866. document.getElementById('btnPath').addEventListener('click', function(){
  867.   showPath = !showPath;
  868.   this.classList.toggle('active', showPath);
  869.   refresh();
  870. });
  871.  
  872. document.querySelectorAll('#pathTypeGroup .pill').forEach(btn=>{
  873.   btn.addEventListener('click', function(){
  874.     pathType = this.getAttribute('data-type');
  875.     document.querySelectorAll('#pathTypeGroup .pill').forEach(b=>b.classList.remove('active'));
  876.     this.classList.add('active');
  877.     refresh();
  878.   });
  879. });
  880.  
  881. const swatchPathEl = document.getElementById('swatchPath');
  882. const notationPathEl = document.getElementById('notationPath');
  883. const pathScrubEl = document.getElementById('pathScrub');
  884. const btnPlayPauseEl = document.getElementById('btnPlayPause');
  885.  
  886. btnPlayPauseEl.addEventListener('click', function(){
  887.   pathPlaying = !pathPlaying;
  888.   this.textContent = pathPlaying ? 'pause' : 'play';
  889.   this.classList.toggle('active', pathPlaying);
  890. });
  891.  
  892. pathScrubEl.addEventListener('input', function(){
  893.   pathPlaying = false;
  894.   btnPlayPauseEl.textContent = 'play';
  895.   btnPlayPauseEl.classList.remove('active');
  896.   pathProgress = parseFloat(this.value)/100;
  897. });
  898.  
  899. function setViewMode(mode){
  900.   viewMode = mode;
  901.   solidMesh.visible = (mode==='solid');
  902.   wireMesh.visible = (mode==='wire');
  903.   pointsMesh.visible = (mode==='points');
  904.   ['btnSolid','btnWire','btnPoints'].forEach(id=>document.getElementById(id).classList.remove('active'));
  905.   document.getElementById('btn'+mode[0].toUpperCase()+mode.slice(1)).classList.add('active');
  906. }
  907. document.getElementById('btnSolid').addEventListener('click', ()=>setViewMode('solid'));
  908. document.getElementById('btnWire').addEventListener('click', ()=>setViewMode('wire'));
  909. document.getElementById('btnPoints').addEventListener('click', ()=>setViewMode('points'));
  910.  
  911. function setRotateBtn(){
  912.   document.getElementById('btnRotate').classList.toggle('active', autoRotate);
  913. }
  914. document.getElementById('btnRotate').addEventListener('click', function(){
  915.   autoRotate = !autoRotate;
  916.   setRotateBtn();
  917. });
  918.  
  919. refresh();
  920.  
  921. /* ---------------------------------------------------------------
  922.    ANIMATION LOOP
  923. ------------------------------------------------------------------*/
  924. const clock = new THREE.Clock();
  925. function animate(){
  926.   requestAnimationFrame(animate);
  927.   const dt = clock.getDelta();
  928.   const t = clock.getElapsedTime();
  929.  
  930.   if(autoRotate && !dragging){
  931.    camTheta += dt*0.12;
  932.     updateCamera();
  933.   }
  934.  
  935.   ringA.lookAt(camera.position);
  936.   ringB.lookAt(camera.position);
  937.  
  938.   if(cometMesh && pathA && pathB && showPath){
  939.    if(pathPlaying){
  940.      pathProgress += dt*0.22*pathDirection;
  941.       if(pathProgress>=1){ pathProgress=1; pathDirection=-1; }
  942.       if(pathProgress<=0){ pathProgress=0; pathDirection=1; }
  943.      pathScrubEl.value = (pathProgress*100).toFixed(1);
  944.    }
  945.    const sample = samplePath(pathA, pathB, pathProgress, pathType);
  946.    cometMesh.visible = true;
  947.    cometMesh.position.copy(sample.pos);
  948.    cometMesh.material.color.setRGB(sample.rgb[0], sample.rgb[1], sample.rgb[2]);
  949.    const s = 1 + Math.sin(t*6)*0.12;
  950.    cometMesh.scale.set(s,s,s);
  951.  
  952.    swatchPathEl.style.background = rgbToHex(sample.rgb);
  953.    notationPathEl.textContent = formatNotationFromAngle(sample.hue, sample.value, sample.chroma);
  954.  } else if(cometMesh){
  955.    cometMesh.visible = false;
  956.  }
  957.  
  958.  renderer.render(scene, camera);
  959. }
  960. animate();
  961.  
  962. })();
  963. </script>
  964. </body>
  965. </html>
  966.  
Advertisement
Add Comment
Please, Sign In to add comment