@@ -2314,36 +2314,23 @@ Converting HSL colors to sRGB colors</h3>
23142314
23152315 Converting an HSL color to sRGB is straightforward mathematically.
23162316 Here's a simple implementation of the conversion algorithm in JavaScript.
2317- For simplicity, this algorithm assumes that the hue has been normalized to
2318- a number in the half-open range [0, 6),
2319- and the saturation and lightness have been normalized to the range [0, 1] .
23202317 It returns an array of three numbers
23212318 representing the red, green, and blue channels of the colors,
23222319 normalized to the range [0, 1] .
23232320
2324- <pre class='lang-javascript'>
2325- function hslToRgb(hue, sat, light) {
2326- if( light <= .5 ) {
2327- var t2 = light * (sat + 1);
2328- } else {
2329- var t2 = light + sat - (light * sat);
2330- }
2331- var t1 = light * 2 - t2;
2332- var r = hueToRgb(t1, t2, hue + 2);
2333- var g = hueToRgb(t1, t2, hue);
2334- var b = hueToRgb(t1, t2, hue - 2);
2335- return [r,g,b] ;
2336- }
2321+ <pre class="include-code lang-javascript">
2322+ path: hslToRgb.js
2323+ highlight: js
2324+ </pre>
23372325
2338- function hueToRgb(t1, t2, hue) {
2339- if(hue < 0) hue += 6;
2340- if(hue >= 6) hue -= 6;
2326+ <h3 id='rgb-to-hsl'>
2327+ Converting sRGB colors to HSL colors</h3>
23412328
2342- if(hue < 1) return (t2 - t1) * hue + t1;
2343- else if(hue < 3) return t2;
2344- else if(hue < 4) return (t2 - t1) * (4 - hue) + t1;
2345- else return t1;
2346- }
2329+ Conversion in the reverse direction proceeds similarly.
2330+
2331+ <pre class="include-code lang-javascript">
2332+ path: rgbToHsl.js
2333+ highlight: js
23472334 </pre>
23482335
23492336<h3 id='hsl-examples'>
@@ -2709,35 +2696,19 @@ Converting HWB colors to sRGB colors</h3>
27092696 first normalizes the white and black components,
27102697 so their sum is no larger than 100%.
27112698
2712- <pre class="lang-javascript">
2713- white /= 100;
2714- black /= 100;
2715- if (white + black >= 1) {
2716- var gray = white / (white + black);
2717- return [gray, gray, gray] ;
2718- }
2719- function hwbToRgb(hue, white, black) {
2720- var rgb = hslToRgb(hue, 1, .5);
2721- for(var i = 0; i < 3; i++) {
2722- rgb[i] *= (1 - white - black);
2723- rgb[i] += white;
2724- }
2725- return rgb;
2726- }
2699+ <pre class="include-code lang-javascript">
2700+ path: hwbToRgb.js
2701+ highlight: js
27272702 </pre>
27282703
27292704<h3 id="rgb-to-hwb">Converting sRGB colors to HWB colors</h3>
27302705
27312706 Conversion in the reverse direction proceeds similarly.
27322707
2733- <pre class="lang-javascript">
2734- function rgbToHwb(red, green, blue) {
2735- var hsl = rgbToHsl(rgb);
2736- var white = Math.min(...rgb);
2737- var black = 1 - Math.max(...rgb);
2738- return([hsl[0] , white*100, black*100]);
2739- }
2740- </pre>
2708+ <pre class="include-code lang-javascript">
2709+ path: rgbToHwb.js
2710+ highlight: js
2711+ </pre>
27412712
27422713<h3 id='hwb-examples'>
27432714Examples of HWB Colors</h3>
0 commit comments