var HSL_HEXcodePlaceholder = '#______'; var HSL_RGBcodePlaceholder = 'rgb(___,___,___)'; var HSL_HSLcodePlaceholder = 'hsl(___,__%,__%)'; var HSL_HSLholder = new Array(); var HSL_RGBholder = new Array(); var HSL_HEXholder = ""; var HSL_width = "wide"; function HSL_GetSwatchNumber() { for( var i=1; i<=4; i++ ) { if( document.getElementById("custom-radio-"+i).checked ) { return i; } } document.getElementById("custom-radio-1").checked = true; return 1; } // function HSL_GetSwatchNumber() function HSL_UpdateSwatch() { var swatch = HSL_GetSwatchNumber(); document.getElementById("custom-hue-"+swatch).innerHTML = HSL_HSLholder[0]; document.getElementById("custom-sat-"+swatch).innerHTML = HSL_HSLholder[1]; document.getElementById("custom-lum-"+swatch).innerHTML = HSL_HSLholder[2]; var hslcolor = "hsl("+HSL_HSLholder[0]+","+HSL_HSLholder[1]+"%,"+HSL_HSLholder[2]+"%)"; document.getElementById("custom-swatch-"+swatch).style.backgroundColor = hslcolor; HSL_UpdateTextHSLcolors(hslcolor); } // function HSL_UpdateSwatch() function HSL_UpdateTextHSLcolors(localcolor) { document.getElementById("text-line-1").style.color = localcolor; document.getElementById("text-line-2").style.color = localcolor; document.getElementById("text-box-border").style.borderColor = localcolor; } // function HSL_UpdateTextColors() function HSL_UpdateTextColors(h,s,l) { var localcolor = "hsl("+h+","+s+"%,"+l+"%)"; HSL_UpdateTextHSLcolors(localcolor); } // function HSL_UpdateTextColors() function HSL_UpdateColorValuesForSelectedSwatch() { var swatch = HSL_GetSwatchNumber(); for(var i=1; i<=4; i++) { if( i == swatch ) { continue; } switch(i) { case 1 : document.getElementById("td-for-1").style.borderLeft="5px solid transparent"; document.getElementById("td-for-1").style.borderTop="5px solid transparent"; break; case 2 : document.getElementById("td-for-2").style.borderRight="5px solid transparent"; document.getElementById("td-for-2").style.borderTop="5px solid transparent"; break; case 3 : document.getElementById("td-for-3").style.borderLeft="5px solid transparent"; document.getElementById("td-for-3").style.borderBottom="5px solid transparent"; break; case 4 : document.getElementById("td-for-4").style.borderRight="5px solid transparent"; document.getElementById("td-for-4").style.borderBottom="5px solid transparent"; break; } } switch(swatch) { case 1 : document.getElementById("td-for-1").style.borderLeft="5px solid black"; document.getElementById("td-for-1").style.borderTop="5px solid black"; break; case 2 : document.getElementById("td-for-2").style.borderRight="5px solid black"; document.getElementById("td-for-2").style.borderTop="5px solid black"; break; case 3 : document.getElementById("td-for-3").style.borderLeft="5px solid black"; document.getElementById("td-for-3").style.borderBottom="5px solid black"; break; case 4 : document.getElementById("td-for-4").style.borderRight="5px solid black"; document.getElementById("td-for-4").style.borderBottom="5px solid black"; break; } var hue = document.getElementById("custom-hue-"+swatch).innerHTML; var sat = document.getElementById("custom-sat-"+swatch).innerHTML; var lum = document.getElementById("custom-lum-"+swatch).innerHTML; if( hue.match(/\d/) && sat.match(/\d/) && lum.match(/\d/) ) { HSL_HueChanged(hue); HSL_SatChanged(sat); HSL_LumChanged(lum); HSL_UpdateTextColors(hue,sat,lum); } } // function function HSL_UpdateColorValuesForSelectedSwatch() function HSL_UpdateColorBarsNarrow() { var hue = document.getElementById("custom-hue-text-box-narrow").value; for( var i=0; i<=100; i++ ) { document.getElementById("sat-change-bar-narrow-"+i).style.backgroundColor="hsl("+hue+","+i+"%,50%)"; document.getElementById("lum-change-bar-narrow-"+i).style.backgroundColor="hsl("+hue+",100%,"+i+"%)"; } } // function HSL_UpdateColorBarsNarrow() function HSL_UpdateColorBarsWide() { var hue = document.getElementById("custom-hue-text-box-wide").value; for( var i=0; i<=100; i++ ) { document.getElementById("sat-change-bar-wide-"+i).style.backgroundColor="hsl("+hue+","+i+"%,50%)"; document.getElementById("lum-change-bar-wide-"+i).style.backgroundColor="hsl("+hue+",100%,"+i+"%)"; } } // function HSL_UpdateColorBarsWide() function HSL_UpdateHUEColorBarMarker(n) { var markerOffset = -4; document.getElementById("custom-hue-pointer-narrow").style.top = parseInt(n+markerOffset)+"px"; markerOffset = 3; if(n>300){markerOffset=4;} document.getElementById("custom-hue-pointer-wide").style.marginLeft = parseInt(n-markerOffset)+"px"; } // function UpdateColorBarMarker(n) function HSL_UpdateSATColorBarMarker(n) { var markerOffset = -4; document.getElementById("custom-sat-pointer-narrow").style.top = parseInt((n*3)+markerOffset)+"px"; markerOffset = 4; markerOffset -= parseInt(n/40); document.getElementById("custom-sat-pointer-wide").style.marginLeft = parseInt((n*3)-markerOffset)+"px"; } // function HSL_UpdateSATColorBarMarker(n) function HSL_UpdateLUMColorBarMarker(n) { var markerOffset = -4; document.getElementById("custom-lum-pointer-narrow").style.top = parseInt((n*3)+markerOffset)+"px"; markerOffset = 4; markerOffset -= parseInt(n/40); document.getElementById("custom-lum-pointer-wide").style.marginLeft = parseInt((n*3)-markerOffset)+"px"; } // function HSL_UpdateLUMColorBarMarker(n) function UpdateHUEspecBox(n) { document.getElementById("custom-hue-text-box-narrow").value = n; document.getElementById("custom-hue-text-box-wide").value = n; } // function UpdateHUEspecBox() function HSL_UpdateSATspecBox(n) { document.getElementById("custom-sat-text-box-narrow").value = n; document.getElementById("custom-sat-text-box-wide").value = n; } // function HSL_UpdateSATspecBox() function HSL_UpdateLUMspecBox(n) { document.getElementById("custom-lum-text-box-narrow").value = n; document.getElementById("custom-lum-text-box-wide").value = n; } // function HSL_UpdateLUMspecBox() function HSL_UpdateRGBcodeDiv() { document.getElementById("rgb-placeholder").innerHTML = "rgb("+HSL_RGBholder[0]+","+HSL_RGBholder[1]+","+HSL_RGBholder[2]+")"; } // function HSL_UpdateRGBcodeDiv() function HSL_UpdateHEXcodeDiv() { document.getElementById("hex-placeholder").innerHTML = "#"+HSL_HEXholder; } // function HSL_UpdateHEXcodeDiv() function HSL_UpdateHSLcodeDiv() { document.getElementById("hsl-placeholder").innerHTML = "hsl("+HSL_HSLholder[0]+","+HSL_HSLholder[1]+"%,"+HSL_HSLholder[2]+"%)"; } // function HSL_UpdateHSLcodeDiv() function HSL_HueChanged(n) { if( n < 0 ) { if( HSL_width == "wide" ) { n = document.getElementById("custom-hue-text-box-wide").value; document.getElementById("custom-hue-text-box-narrow").value = n; } else { n = document.getElementById("custom-hue-text-box-narrow").value; document.getElementById("custom-hue-text-box-wide").value = n; } } n = String(n); n = n.replace(/\D/g,""); if( ! n.match(/\d/) ) { n = 0; } n = parseInt(n); if( n > 360 ) { n = 360; } if( n < 0 ) { n = 0; } UpdateHUEspecBox(n); /* Do this before HSL_CalculateFromHSL() */ HSL_CalculateFromHSL(n,document.getElementById("custom-sat-text-box-narrow").value,document.getElementById("custom-lum-text-box-narrow").value); HSL_CalculateFromHSL(n,document.getElementById("custom-sat-text-box-wide").value,document.getElementById("custom-lum-text-box-wide").value); HSL_UpdateHUEColorBarMarker(n); HSL_UpdateColorBarsNarrow(); HSL_UpdateColorBarsWide(); HSL_UpdateRGBcodeDiv(); HSL_UpdateHEXcodeDiv(); HSL_UpdateHSLcodeDiv(); HSL_UpdateSwatch(); } // function HSL_HueChanged(n) function HSL_SatChanged(n) { if( n < 0 ) { if( HSL_width == "wide" ) { n = document.getElementById("custom-sat-text-box-wide").value; document.getElementById("custom-sat-text-box-narrow").value = n; } else { n = document.getElementById("custom-sat-text-box-narrow").value; document.getElementById("custom-sat-text-box-wide").value = n; } } n = String(n); n = n.replace(/[^\d\.]/g,""); if( ! n.match(/\d/) ) { n = 0; } n = parseFloat(n); if( n > 100 ) { n = 100; } if( n < 0 ) { n = 0; } HSL_UpdateSATspecBox(n); /* Do this before HSL_CalculateFromHSL() */ HSL_CalculateFromHSL(document.getElementById("custom-hue-text-box-narrow").value,n,document.getElementById("custom-lum-text-box-narrow").value); HSL_CalculateFromHSL(document.getElementById("custom-hue-text-box-wide").value,n,document.getElementById("custom-lum-text-box-wide").value); HSL_UpdateSATColorBarMarker(n); HSL_UpdateColorBarsNarrow(); HSL_UpdateColorBarsWide(); HSL_UpdateRGBcodeDiv(); HSL_UpdateHEXcodeDiv(); HSL_UpdateHSLcodeDiv(); HSL_UpdateSwatch(); } // function HSL_SatChanged(n) function HSL_LumChanged(n) { if( n < 0 ) { if( HSL_width == "wide" ) { n = document.getElementById("custom-lum-text-box-wide").value; document.getElementById("custom-lum-text-box-narrow").value = n; } else { n = document.getElementById("custom-lum-text-box-narrow").value; document.getElementById("custom-lum-text-box-wide").value = n; } } n = String(n); n = n.replace(/[^\d\.]/g,""); if( ! n.match(/\d/) ) { n = 0; } n = parseFloat(n); if( n > 100 ) { n = 100; } if( n < 0 ) { n = 0; } HSL_UpdateLUMspecBox(n); /* Do this before HSL_CalculateFromHSL() */ HSL_CalculateFromHSL(document.getElementById("custom-hue-text-box-narrow").value,document.getElementById("custom-sat-text-box-narrow").value,n); HSL_CalculateFromHSL(document.getElementById("custom-hue-text-box-wide").value,document.getElementById("custom-sat-text-box-wide").value,n); HSL_UpdateLUMColorBarMarker(n); HSL_UpdateColorBarsNarrow(); HSL_UpdateColorBarsWide(); HSL_UpdateRGBcodeDiv(); HSL_UpdateHEXcodeDiv(); HSL_UpdateHSLcodeDiv(); HSL_UpdateSwatch(); } // function HSL_LumChanged(n) function HSL_UpdateValuesIfValidRGB(d) { var rgb = d.innerHTML.replace(/<[^>]*>/g,""); rgb = rgb.replace(/[^\d,]/g,""); var ta = rgb.split(","); if( ta.length != 3 ) { return; } for( var i = 0; i <= 2; i++ ) { if( ta[i] > 255 ) { ta[i] = 255; } if( ta[i] < 0 ) { ta[i] = 0; } } HSL_RGBholder[0] = ta[0]; HSL_RGBholder[1] = ta[1]; HSL_RGBholder[2] = ta[2]; HSL_CalculateRGB2HEX(HSL_RGBholder[0],HSL_RGBholder[1],HSL_RGBholder[2]); HSL_CalculateRGB2HSL(HSL_RGBholder[0],HSL_RGBholder[1],HSL_RGBholder[2]); document.getElementById("custom-hue-text-box-wide").value = HSL_HSLholder[0]; document.getElementById("custom-sat-text-box-wide").value = HSL_HSLholder[1]; document.getElementById("custom-lum-text-box-wide").value = HSL_HSLholder[2]; document.getElementById("custom-hue-text-box-narrow").value = HSL_HSLholder[0]; document.getElementById("custom-sat-text-box-narrow").value = HSL_HSLholder[1]; document.getElementById("custom-lum-text-box-narrow").value = HSL_HSLholder[2]; HSL_UpdateHUEColorBarMarker(HSL_HSLholder[0]); HSL_UpdateSATColorBarMarker(HSL_HSLholder[1]); HSL_UpdateLUMColorBarMarker(HSL_HSLholder[2]); HSL_UpdateColorBarsNarrow(); HSL_UpdateColorBarsWide(); HSL_UpdateRGBcodeDiv(); HSL_UpdateHEXcodeDiv(); HSL_UpdateHSLcodeDiv(); HSL_UpdateSwatch(); } // function HSL_UpdateValuesIfValidRGB() function HSL_UpdateValuesIfValidHEX(d) { var h = d.innerHTML.replace(/<[^>]*>/g,""); var h = h.replace(/ /g,""); h = h.replace(/[^0-9a-fA-F]/g,""); if( h.length == 3 ) { h = h.substr(0,1) + h.substr(0,1) + h.substr(1,1) + h.substr(1,1) + h.substr(2,1) + h.substr(2,1); } if( h.length != 6 ) { return; } HSL_HEXholder = h; HSL_CalculateHEX2RGB(HSL_HEXholder); HSL_CalculateRGB2HSL(HSL_RGBholder[0],HSL_RGBholder[1],HSL_RGBholder[2]); document.getElementById("custom-hue-text-box-wide").value = HSL_HSLholder[0]; document.getElementById("custom-sat-text-box-wide").value = HSL_HSLholder[1]; document.getElementById("custom-lum-text-box-wide").value = HSL_HSLholder[2]; document.getElementById("custom-hue-text-box-narrow").value = HSL_HSLholder[0]; document.getElementById("custom-sat-text-box-narrow").value = HSL_HSLholder[1]; document.getElementById("custom-lum-text-box-narrow").value = HSL_HSLholder[2]; HSL_UpdateHUEColorBarMarker(HSL_HSLholder[0]); HSL_UpdateSATColorBarMarker(HSL_HSLholder[1]); HSL_UpdateLUMColorBarMarker(HSL_HSLholder[2]); HSL_UpdateColorBarsNarrow(); HSL_UpdateColorBarsWide(); HSL_UpdateRGBcodeDiv(); HSL_UpdateHEXcodeDiv(); HSL_UpdateHSLcodeDiv(); HSL_UpdateSwatch(); } // function HSL_UpdateValuesIfValidHEX() function HSL_UpdateValuesIfValidHSL(d) { var hsl = d.innerHTML.replace(/<[^>]*>/g,""); hsl = hsl.replace(/[^\d,\.]/g,""); var ta = hsl.split(","); if( ta.length != 3 ) { return; } if( ta[0] > 360 ) { ta[0] = 360; } if( ta[0] < 0 ) { ta[0] = 0; } for( var i = 1; i <= 2; i++ ) { if( ta[i] > 100 ) { ta[i] = 100; } if( ta[i] < 0 ) { ta[i] = 0; } } HSL_HSLholder[0] = ta[0]; HSL_HSLholder[1] = ta[1]; HSL_HSLholder[2] = ta[2]; HSL_CalculateHSL2RGB(HSL_HSLholder[0],HSL_HSLholder[1],HSL_HSLholder[2]); HSL_CalculateRGB2HEX(HSL_RGBholder[0],HSL_RGBholder[1],HSL_RGBholder[2]); document.getElementById("custom-hue-text-box-wide").value = HSL_HSLholder[0]; document.getElementById("custom-sat-text-box-wide").value = HSL_HSLholder[1]; document.getElementById("custom-lum-text-box-wide").value = HSL_HSLholder[2]; document.getElementById("custom-hue-text-box-narrow").value = HSL_HSLholder[0]; document.getElementById("custom-sat-text-box-narrow").value = HSL_HSLholder[1]; document.getElementById("custom-lum-text-box-narrow").value = HSL_HSLholder[2]; HSL_UpdateHUEColorBarMarker(HSL_HSLholder[0]); HSL_UpdateSATColorBarMarker(HSL_HSLholder[1]); HSL_UpdateLUMColorBarMarker(HSL_HSLholder[2]); HSL_UpdateColorBarsNarrow(); HSL_UpdateColorBarsWide(); HSL_UpdateRGBcodeDiv(); HSL_UpdateHEXcodeDiv(); HSL_UpdateHSLcodeDiv(); HSL_UpdateSwatch(); } // function HSL_UpdateValuesIfValidHSL() function HSL_CalculateHEX2RGB(h) { HSL_RGBholder = Array(); HSL_RGBholder.push(parseInt(h.substring(0,2),16)); HSL_RGBholder.push(parseInt(h.substring(2,4),16)); HSL_RGBholder.push(parseInt(h.substring(4,6),16)); } // function HSL_CalculateHEX2RGB() function HSL_CalculateRGB2HEX(r,g,b) { HSL_HEXholder = ""; r = parseInt(r); g = parseInt(g); b = parseInt(b); var hexpart = new String(); hexpart = r.toString(16); HSL_HEXholder += (hexpart.length < 2 ? "0" + hexpart : hexpart); hexpart = g.toString(16); HSL_HEXholder += (hexpart.length < 2 ? "0" + hexpart : hexpart); hexpart = b.toString(16); HSL_HEXholder += (hexpart.length < 2 ? "0" + hexpart : hexpart); } // function HSL_CalculateRGB2HEX() function HSL_CalculateRGB2HSL(r,g,b) { /* Modified code from: stackoverflow.com/questions/46432335/hex-to-hsl-convert-javascript */ r /= 255, g /= 255, b /= 255; var max = Math.max(r, g, b), min = Math.min(r, g, b); var h, s, l = (max + min) / 2; if(max == min) { h = s = 0; } else { var 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; case b: h = (r - g) / d + 4; break; } h /= 6; } s *= 100; s = Math.round((s*100)+.5); s /= 100; var ss = "" + s; if( ss.indexOf(".0") > -1 || ss.indexOf(".9") > -1 ) { s = Math.round(s); } l *= 100; l = Math.round((l*100)+.5); l /= 100; ss = "" + l; if( ss.indexOf(".0") > -1 || ss.indexOf(".9") > -1 ) { l = Math.round(l); } h = Math.round(360*h); HSL_HSLholder = Array(); HSL_HSLholder.push(h); HSL_HSLholder.push(s); HSL_HSLholder.push(l); } // function HSL_CalculateRGB2HSL() function HSL_CalculateHSL2RGB(h,s,l) { /* Modified code from: stackoverflow.com/questions/36721830/convert-hsl-to-rgb-and-hex/54014428#54014428 */ h /= 360; s /= 100; l /= 100; 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); } HSL_RGBholder = Array(); HSL_RGBholder.push(Math.round(r*255)); HSL_RGBholder.push(Math.round(g*255)); HSL_RGBholder.push(Math.round(b*255)); } // function HSL_CalculateHSL2RGB() function HSL_CalculateFromRGB(r,g,b) { HSL_RGBholder = Array(); HSL_RGBholder.push(r); HSL_RGBholder.push(g); HSL_RGBholder.push(b); HSL_CalculateRGB2HEX(r,g,b); HSL_CalculateRGB2HSL(r,g,b); } // function HSL_CalculateFromRGB() function HSL_CalculateFromHEX(h) { HSL_HEXholder = h; HSL_CalculateHEX2RGB(HSL_HEXholder); HSL_CalculateRGB2HSL(HSL_RGBholder[0],HSL_RGBholder[1],HSL_RGBholder[2]); } // function HSL_CalculateFromHEX() function HSL_CalculateFromHSL(h,s,l) { HSL_HSLholder = Array(); HSL_HSLholder.push(h); HSL_HSLholder.push(s); HSL_HSLholder.push(l); HSL_CalculateHSL2RGB(h,s,l); HSL_CalculateRGB2HEX(HSL_RGBholder[0],HSL_RGBholder[1],HSL_RGBholder[2]); } // function HSL_CalculateFromHSL() function HSL_InitializeOnPageLoad() { HSL_CalculateFromHSL(0,100,50); HSL_GetSwatchNumber(); /* To ensure a radio button is checked. */ UpdateHUEspecBox(HSL_HSLholder[0]); /* Do these before Update...ColorBarMarker...(n) and HSL_UpdateColorBars...() */ HSL_UpdateSATspecBox(HSL_HSLholder[1]); HSL_UpdateLUMspecBox(HSL_HSLholder[2]); HSL_UpdateHUEColorBarMarker(HSL_HSLholder[0]); HSL_UpdateSATColorBarMarker(HSL_HSLholder[1]); HSL_UpdateLUMColorBarMarker(HSL_HSLholder[2]); HSL_UpdateColorBarsNarrow(); HSL_UpdateColorBarsWide(); HSL_UpdateRGBcodeDiv(); HSL_UpdateHEXcodeDiv(); HSL_UpdateHSLcodeDiv(); HSL_UpdateSwatch(); HSL_MakeThisSwatchLive(1);// Initial load } // function HSL_InitializeOnPageLoad() function HSL_OpenNamedColors() { document.getElementById("named-colors-link").style.display="none"; document.getElementById("named-colors-container").style.display="block"; } // function HSL_OpenNamedColors() function HSL_PlaceNamedColorCodeIntoAppropriateBox(hx) { if( hx.length < 8 ) { var d = document.getElementById("hex-placeholder"); hx = hx.replace(/^\#*/,''); d.innerHTML = "#" + hx; HSL_UpdateValuesIfValidHEX(d); return; } var d = document.getElementById("rgb-placeholder"); d.innerHTML = hx; HSL_UpdateValuesIfValidRGB(d); } // function HSL_PlaceNamedColorCodeIntoAppropriateBox() function HSL_MakeThisSwatchLive(n) { for( var i=1; i<=4; i++ ) { document.getElementById("custom-radio-"+i).checked = false; } document.getElementById("custom-radio-"+n).checked = true; HSL_UpdateColorValuesForSelectedSwatch(); } // function HSL_MakeThisSwatchLive() function HSL_GetHTML() { var url = "https://www.willmaster.com/library/external/ColorSelector4ColorBlind/ColorSelectorAjax.php"; var http = new XMLHttpRequest(); if(! http) { return; } var params = new Array(); params.push( "pg=" + encodeURIComponent(document.URL) ); http.onreadystatechange = function() { if(http.readyState == 4 && http.status == 200) { document.getElementById("color-selector-4-color-blind").innerHTML = http.responseText; HSL_InitializeOnPageLoad(); } } http.open("POST",url,true); http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); http.send(params.join("&")); } // HSL_GetHTML() HSL_GetHTML();