From 64204bd5108a36f18d91dec19a6cd23cc2d52864 Mon Sep 17 00:00:00 2001 From: Estelle Weyl Date: Wed, 15 Mar 2023 02:43:39 -0700 Subject: [PATCH 01/11] New page: color modules example --- modules/colors.html | 180 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 modules/colors.html diff --git a/modules/colors.html b/modules/colors.html new file mode 100644 index 00000000..b5a20d24 --- /dev/null +++ b/modules/colors.html @@ -0,0 +1,180 @@ + + + + + Color picker + + + +
+ + + + + + + + + +
Current color values:
RGB
HEX
HSL
HWB
+ +

+

+

+
+ + + From ca1a78624ce992c24e8c09db87bb06431929413b Mon Sep 17 00:00:00 2001 From: Estelle Weyl Date: Wed, 15 Mar 2023 09:20:13 -0700 Subject: [PATCH 02/11] accent color on form controls --- modules/colors.html | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/modules/colors.html b/modules/colors.html index b5a20d24..a76322e1 100644 --- a/modules/colors.html +++ b/modules/colors.html @@ -160,11 +160,12 @@ const setBackgroundColor = function ( color, opacity ) { const body = document.querySelector( "div" ); - if( opacity == 1 ) { - body.style.backgroundColor = color; - } else { - body.style.backgroundColor = color + "" + hexOpacity( opacity ); + if( opacity != 1 ) { + color = color + "" + hexOpacity( opacity ); } + body.style.backgroundColor = color; + opacityPicker.style.accentColor = color; + colorPicker.style.accentColor = color; } const hexOpacity = function( opacity ) { From de471724b9a1b9caa9bb61db5f0d23d9ccf4c174 Mon Sep 17 00:00:00 2001 From: Estelle Weyl Date: Wed, 15 Mar 2023 13:52:07 -0700 Subject: [PATCH 03/11] write JS a different way --- modules/colors.html | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/modules/colors.html b/modules/colors.html index a76322e1..4cb6ade6 100644 --- a/modules/colors.html +++ b/modules/colors.html @@ -73,7 +73,7 @@ createColor(); }); -const createColor = function() { +function createColor() { const currentColor = colorPicker.value; const currentOpacity = opacityPicker.value; setBackgroundColor( currentColor, currentOpacity ); @@ -81,32 +81,32 @@ createRGB( currentColor, currentOpacity ); } -const createHEX = function ( color, opacity ) { +function createHEX ( color, opacity ) { const cell = document.querySelector( "#HEX td" ); if ( opacity == 1 ) { - cell.innerHTML = color; + cell.textContent = color; } else { - cell.innerHTML = color + hexOpacity( opacity ); + cell.textContent = color + hexOpacity( opacity ); } } -const createRGB = function ( color, opacity ) { +function createRGB ( color, opacity ) { const cell = document.querySelector( "#RGB td" ); color = color.substring( 1, 6 ); - let hexArray = color.match( /.{1,2}/g ); - let R = parseInt( hexArray[0], 16 ), - G = parseInt( hexArray[1], 16 ), - B = parseInt( hexArray[2], 16 ); + const hexArray = color.match( /.{1,2}/g ); + const R = parseInt( hexArray[0], 16 ); + const G = parseInt( hexArray[1], 16 ); + const B = parseInt( hexArray[2], 16 ); if( opacity == 1 ) { - cell.innerHTML = `rgb(${R} ${G} ${B})`; + cell.textContent = `rgb(${R} ${G} ${B})`; } else { - cell.innerHTML = `rgb(${R} ${G} ${B} / ${opacity})`; + cell.textContent = `rgb(${R} ${G} ${B} / ${opacity})`; } createHSL( R, G, B, opacity ); } -const createHSL = function ( r, g, b, opacity ) { +function createHSL ( r, g, b, opacity ) { const cell = document.querySelector( "#HSL td" ); r = r / 255, g = g / 255, @@ -138,27 +138,27 @@ l = +( l * 100 ).toFixed( 1 ); if( opacity == 1 ) { - cell.innerHTML = `hsl(${h} ${s}% ${l}%)`; + cell.textContent = `hsl(${h} ${s}% ${l}%)`; } else { - cell.innerHTML = `hsl(${h} ${s}% ${l}% / ${opacity})` + cell.textContent = `hsl(${h} ${s}% ${l}% / ${opacity})` } createHWB( h, s, l, opacity ); } -const createHWB = function ( h, s, l, opacity ) { +function createHWB ( h, s, l, opacity ) { const cell = document.querySelector( "#HWB td" ); const hsv = s * ( l < 50 ? l : 100 - l ) / 100; const W = hsv === 0 ? 0 : 2 * hsv / ( l + hsv ) * 100; const B = l + hsv; if( opacity == 1 ) { - cell.innerHTML = `hwb(${h} ${W.toFixed( 1 )}% ${B.toFixed( 1 )}%)`; + cell.textContent = `hwb(${h} ${W.toFixed( 1 )}% ${B.toFixed( 1 )}%)`; } else { - cell.innerHTML = `hwb(${h} ${W.toFixed( 1 )}% ${B.toFixed( 1 )}% / ${opacity})`; + cell.textContent = `hwb(${h} ${W.toFixed( 1 )}% ${B.toFixed( 1 )}% / ${opacity})`; } } -const setBackgroundColor = function ( color, opacity ) { +function setBackgroundColor ( color, opacity ) { const body = document.querySelector( "div" ); if( opacity != 1 ) { color = color + "" + hexOpacity( opacity ); @@ -168,14 +168,14 @@ colorPicker.style.accentColor = color; } -const hexOpacity = function( opacity ) { - if( opacity > 0 ) { +function hexOpacity( opacity ) { + if( opacity > 0 ){ return Math.floor( opacity * 255 ).toString( 16 ); } else { return "00"; } -} +} From 11060ca9e54abce5a3372a92ced3018f87690a6e Mon Sep 17 00:00:00 2001 From: Estelle Weyl Date: Wed, 15 Mar 2023 14:07:53 -0700 Subject: [PATCH 04/11] prettier --- modules/colors.html | 355 ++++++++++++++++++++++++-------------------- 1 file changed, 190 insertions(+), 165 deletions(-) diff --git a/modules/colors.html b/modules/colors.html index 4cb6ade6..bc78120a 100644 --- a/modules/colors.html +++ b/modules/colors.html @@ -4,178 +4,203 @@ Color picker - +
- + - + - - - - + + + + + + + + + + + + + + + +
Current color values: + Current color values: +
RGB
HEX
HSL
HWB
RGB
HEX
HSL
HWB
- -

-

+ +

+ +

+

+ +

- - + + From da98dd5519bf3bb5ea14d838b0af91709a17c991 Mon Sep 17 00:00:00 2001 From: Estelle Weyl Date: Wed, 15 Mar 2023 22:07:15 -0700 Subject: [PATCH 05/11] oopsie fixed --- modules/colors.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/colors.html b/modules/colors.html index bc78120a..e08c8dba 100644 --- a/modules/colors.html +++ b/modules/colors.html @@ -124,7 +124,7 @@ function createRGB(color, opacity) { const cell = document.querySelector("#RGB td"); - color = color.substring(1, 6); + color = color.substring(1, 7); const hexArray = color.match(/.{1,2}/g); const R = parseInt(hexArray[0], 16); const G = parseInt(hexArray[1], 16); From e810473e400976ee9678b7bab9ee9fd966147236 Mon Sep 17 00:00:00 2001 From: Estelle Weyl Date: Thu, 16 Mar 2023 10:28:02 -0700 Subject: [PATCH 06/11] stringify hexopacity --- modules/colors.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/colors.html b/modules/colors.html index e08c8dba..9bf35551 100644 --- a/modules/colors.html +++ b/modules/colors.html @@ -118,7 +118,7 @@ if (opacity === 1) { cell.textContent = color; } else { - cell.textContent = color + hexOpacity(opacity); + cell.textContent = `${color}${hexOpacity(opacity)}`; } } From 35906ee1c9c68bcf297279e19e28ce3cb7aa85de Mon Sep 17 00:00:00 2001 From: Estelle Weyl Date: Thu, 16 Mar 2023 10:35:44 -0700 Subject: [PATCH 07/11] make re-used code a function --- modules/colors.html | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/colors.html b/modules/colors.html index 9bf35551..7b1f26c4 100644 --- a/modules/colors.html +++ b/modules/colors.html @@ -118,7 +118,7 @@ if (opacity === 1) { cell.textContent = color; } else { - cell.textContent = `${color}${hexOpacity(opacity)}`; + cell.textContent = hexOpacity(color, opacity); } } @@ -187,19 +187,19 @@ function setBackgroundColor(color, opacity) { const body = document.querySelector("div"); if (opacity !== 1) { - color = color + "" + hexOpacity(opacity); + color = hexOpacity(color, opacity); } body.style.backgroundColor = color; opacityPicker.style.accentColor = color; colorPicker.style.accentColor = color; } - function hexOpacity(opacity) { + function hexOpacity(color, opacity) { + let char = "00" if (opacity > 0) { - return Math.floor(opacity * 255).toString(16); - } else { - return "00"; + char = Math.floor(opacity * 255).toString(16); } + return `${color}${char}` } From b7711ca9cdc5759d20058707432115973a670e75 Mon Sep 17 00:00:00 2001 From: Estelle Weyl Date: Thu, 16 Mar 2023 10:36:35 -0700 Subject: [PATCH 08/11] prettier --- modules/colors.html | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/modules/colors.html b/modules/colors.html index 7b1f26c4..ab32f3c2 100644 --- a/modules/colors.html +++ b/modules/colors.html @@ -27,7 +27,13 @@ font-family: monospace; } body { - background: linear-gradient(-90deg, transparent 0 38%, 47%, rgb(0 0 0 / 0.7) 50%, white 50% 100%), + background: linear-gradient( + -90deg, + transparent 0 38%, + 47%, + rgb(0 0 0 / 0.7) 50%, + white 50% 100% + ), conic-gradient( black 0 90deg, transparent 90deg 180deg, @@ -195,11 +201,11 @@ } function hexOpacity(color, opacity) { - let char = "00" + let char = "00"; if (opacity > 0) { - char = Math.floor(opacity * 255).toString(16); + char = Math.floor(opacity * 255).toString(16); } - return `${color}${char}` + return `${color}${char}`; } From 614929adb16a6bb305601a2fe570f604b2c1a4e4 Mon Sep 17 00:00:00 2001 From: Estelle Weyl Date: Thu, 16 Mar 2023 10:52:15 -0700 Subject: [PATCH 09/11] js syntax === and () around tertiaries --- modules/colors.html | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/modules/colors.html b/modules/colors.html index ab32f3c2..e2beaf04 100644 --- a/modules/colors.html +++ b/modules/colors.html @@ -136,7 +136,7 @@ const G = parseInt(hexArray[1], 16); const B = parseInt(hexArray[2], 16); - if (opacity == 1) { + if (opacity === 1) { cell.textContent = `rgb(${R} ${G} ${B})`; } else { cell.textContent = `rgb(${R} ${G} ${B} / ${opacity})`; @@ -154,9 +154,9 @@ s = 0, l = 0; - if (delta == 0) h = 0; - else if (cmax == r) h = ((g - b) / delta) % 6; - else if (cmax == g) h = (b - r) / delta + 2; + if (delta === 0) h = 0; + else if (cmax === r) h = ((g - b) / delta) % 6; + else if (cmax === g) h = (b - r) / delta + 2; else h = (r - g) / delta + 4; h = Math.round(h * 60); @@ -164,11 +164,11 @@ if (h < 0) h += 360; l = (cmax + cmin) / 2; - s = delta == 0 ? 0 : delta / (1 - Math.abs(2 * l - 1)); + s = (delta === 0) ? 0 : delta / (1 - Math.abs(2 * l - 1)); s = +(s * 100).toFixed(1); l = +(l * 100).toFixed(1); - if (opacity == 1) { + if (opacity === 1) { cell.textContent = `hsl(${h} ${s}% ${l}%)`; } else { cell.textContent = `hsl(${h} ${s}% ${l}% / ${opacity})`; @@ -178,8 +178,8 @@ function createHWB(h, s, l, opacity) { const cell = document.querySelector("#HWB td"); - const hsv = (s * (l < 50 ? l : 100 - l)) / 100; - const W = hsv === 0 ? 0 : ((2 * hsv) / (l + hsv)) * 100; + const hsv = (s * ((l < 50) ? l : 100 - l)) / 100; + const W = (hsv === 0) ? 0 : ((2 * hsv) / (l + hsv)) * 100; const B = l + hsv; if (opacity === 1) { cell.textContent = `hwb(${h} ${W.toFixed(1)}% ${B.toFixed(1)}%)`; From 3aed42a4fb900b402598c741536e77372f404298 Mon Sep 17 00:00:00 2001 From: Estelle Weyl Date: Fri, 17 Mar 2023 12:12:13 -0700 Subject: [PATCH 10/11] Update modules/colors.html Co-authored-by: Jean-Yves Perrier --- modules/colors.html | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/colors.html b/modules/colors.html index e2beaf04..33e867d9 100644 --- a/modules/colors.html +++ b/modules/colors.html @@ -179,14 +179,14 @@ function createHWB(h, s, l, opacity) { const cell = document.querySelector("#HWB td"); const hsv = (s * ((l < 50) ? l : 100 - l)) / 100; - const W = (hsv === 0) ? 0 : ((2 * hsv) / (l + hsv)) * 100; - const B = l + hsv; + let W = (hsv === 0) ? 0 : ((2 * hsv) / (l + hsv)) * 100; + let B = l + hsv; + W = W.toFixed(1); + B = B.toFixed(1); if (opacity === 1) { - cell.textContent = `hwb(${h} ${W.toFixed(1)}% ${B.toFixed(1)}%)`; + cell.textContent = `hwb(${h} ${W}% ${B}%)`; } else { - cell.textContent = `hwb(${h} ${W.toFixed(1)}% ${B.toFixed( - 1 - )}% / ${opacity})`; + cell.textContent = `hwb(${h} ${W}% ${B}% / ${opacity})`; } } From fb7cad5fa2de6ecda5bc792b38032ead71abb2a1 Mon Sep 17 00:00:00 2001 From: Estelle Weyl Date: Fri, 17 Mar 2023 12:13:19 -0700 Subject: [PATCH 11/11] Apply suggestions from code review Co-authored-by: Jean-Yves Perrier --- modules/colors.html | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/modules/colors.html b/modules/colors.html index 33e867d9..0f1cef5b 100644 --- a/modules/colors.html +++ b/modules/colors.html @@ -113,7 +113,7 @@ function createColor() { const currentColor = colorPicker.value; - const currentOpacity = opacityPicker.value * 1; + const currentOpacity = Number(opacityPicker.value); setBackgroundColor(currentColor, currentOpacity); createHEX(currentColor, currentOpacity); createRGB(currentColor, currentOpacity); @@ -146,7 +146,10 @@ function createHSL(r, g, b, opacity) { const cell = document.querySelector("#HSL td"); - (r = r / 255), (g = g / 255), (b = b / 255); + // Let's have r, g, b in the range [0, 1] + r = r / 255; + g = g / 255; + b = b / 255; const cmin = Math.min(r, g, b); const cmax = Math.max(r, g, b); const delta = cmax - cmin; @@ -154,19 +157,26 @@ s = 0, l = 0; - if (delta === 0) h = 0; - else if (cmax === r) h = ((g - b) / delta) % 6; - else if (cmax === g) h = (b - r) / delta + 2; + if (delta === 0) { + h = 0; + } else if (cmax === r) { + h = ((g - b) / delta) % 6; + } else if (cmax === g) { + h = (b - r) / delta + 2; + } else h = (r - g) / delta + 4; h = Math.round(h * 60); - if (h < 0) h += 360; + // We want an angle between 0 and 360° + if (h < 0) { + h += 360; + } l = (cmax + cmin) / 2; s = (delta === 0) ? 0 : delta / (1 - Math.abs(2 * l - 1)); - s = +(s * 100).toFixed(1); - l = +(l * 100).toFixed(1); + s = Number((s * 100).toFixed(1)); + l = Number((l * 100).toFixed(1)); if (opacity === 1) { cell.textContent = `hsl(${h} ${s}% ${l}%)`;