### Import and Initialize Color Parsing Source: https://github.com/myndex/colorparsley/blob/master/src/test.html Imports necessary functions from the colorparsley library and sets up an output element for displaying test results. This is the initial setup for running the color parsing tests. ```javascript import { colorParsley, colorToHex, colorToRGB } from './colorparsley.js'; let outputText = document.getElementById('test'); outputText.innerHTML = '
Colors may be a hex string, a named color, an rgb(), or just a number
' ``` -------------------------------- ### Color Pipeline Example with colorParsley, colorToHex, and colorToRGB Source: https://context7.com/myndex/colorparsley/llms.txt Demonstrates a common use case for Color Parsley by creating a `normalizeColor` function that parses input, converts it to hex and rgb strings, and returns an object with the color details and colorspace. Handles invalid inputs by returning null. ```javascript import { colorParsley, colorToHex, colorToRGB } from 'colorparsley'; // Building a color pipeline function normalizeColor(input) { const parsed = colorParsley(input); if (!parsed[4]) return null; // isValid === false return { hex: colorToHex(parsed), // e.g. '5f9ea0' rgb: colorToRGB(parsed), // e.g. 'rgb(95,158,160)' array: parsed, // e.g. [95, 158, 160, 1, true, 'sRGB'] colorspace: parsed[5], // e.g. 'sRGB' }; } normalizeColor('cadetblue'); // { hex: '5f9ea0', rgb: 'rgb(95,158,160)', array: [...], colorspace: 'sRGB' } normalizeColor('invalid!!'); // null ``` -------------------------------- ### Check Color Validity using colorParsley Result Source: https://context7.com/myndex/colorparsley/llms.txt Provides an example of how to check the validity of a parsed color and access its components or error information. ```javascript const result = colorParsley(userInput); if (!result[4]) { console.error('Invalid color:', result[5]); // 'parsleyError' or 'inputError' } else { const [r, g, b, a, , colorspace] = result; console.log(`Parsed: r=${r} g=${g} b=${b} a=${a} space=${colorspace}`); } ``` -------------------------------- ### Parse Greyscale Shorthands with colorParsley Source: https://context7.com/myndex/colorparsley/llms.txt Illustrates parsing of greyscale shorthand notations, including integer + comma, percentage, and 2-digit hex. ```javascript colorParsley('123,'); // [123, 123, 123, 1, true, 'sRGB'] (INT + comma → grey) colorParsley('87%'); // [221.85, 221.85, 221.85, 1, true, 'sRGB'] (% → grey) colorParsley('#a7'); // [167, 167, 167, 1, true, 'sRGB'] (2-digit hex → grey) ``` -------------------------------- ### Parse RGB/RGBA Color Strings with colorParsley Source: https://context7.com/myndex/colorparsley/llms.txt Shows how to parse standard rgb() and rgba() color notations, including percentage values and floating-point RGB. ```javascript colorParsley('rgb(123,45,67)'); // [123, 45, 67, 1, true, 'sRGB'] colorParsley('rgba(123,45,67,0.5)'); // [123, 45, 67, 0.5, true, 'sRGB'] colorParsley('rgb(170.0, 187.0, 205.201, 0.7)'); // [170, 187, 205.201, 0.7, true, 'sRGB'] colorParsley('rgba(0,0,0,0)'); // [0, 0, 0, 0, true, 'sRGB'] colorParsley('100%,50%,25%'); // percentage rgb values → 0–255 scaled ``` -------------------------------- ### Parse Hex Color Strings with colorParsley Source: https://context7.com/myndex/colorparsley/llms.txt Demonstrates parsing of various hex string formats, including shorthand, with and without alpha, and optional hash. ```javascript import { colorParsley } from 'colorparsley'; // --- Hex strings --- colorParsley('#123456cc'); // [18, 52, 86, 0.8, true, 'sRGB'] colorParsley('#abc'); // [170, 187, 204, 1, true, 'sRGB'] (expands to #aabbcc) colorParsley('#fa'); // [250, 250, 250, 1, true, 'sRGB'] (greyscale shorthand) colorParsley('abcdef'); // [171, 205, 239, 1, true, 'sRGB'] (hash optional) ``` -------------------------------- ### Passthrough Array Input for colorParsley Source: https://context7.com/myndex/colorparsley/llms.txt Demonstrates that colorParsley will return a valid six-element array input as-is without modification. ```javascript colorParsley([255, 0, 128, 1, true, 'sRGB']); // [255, 0, 128, 1, true, 'sRGB'] (returned as-is) ``` -------------------------------- ### CSS 4 Color Compositing Formulas Source: https://github.com/myndex/colorparsley/blob/master/docs/ColorParsleyRegexChart.html Formulas for simple alpha compositing, including both premultiplied and straight (not premultiplied) color calculations. ```plaintext Er, Eg, Eb - Element color value Ea - Element alpha value Cr, Cg, Cb - Canvas color value (before blending) Ca - Canvas alpha value (before blending) Cr', Cg', Cb' - Canvas color value (after blending) Ca' - Canvas alpha value (after blending) Ca' = 1 - (1 - Ea) * (1 - Ca) Premultiplied colors Cr' = (1 - Ea) * Cr + Er Cg' = (1 - Ea) * Cg + Eg Cb' = (1 - Ea) * Cb + Eb Straight not Premultiplied Cr' = (1 - Ea) * Cr*Ca + Er*Ea Cg' = (1 - Ea) * Cg*Ca + Eg*Ea Cb' = (1 - Ea) * Cb*Ca + Eb*Ea ``` -------------------------------- ### Parse HSL and HWB Colors with colorParsley Source: https://context7.com/myndex/colorparsley/llms.txt Demonstrates parsing of HSL and HWB color formats, with and without alpha values. ```javascript colorParsley('hsl(112, 22.8%, 66.5%)'); // [155, 189, 150, 1, true, 'sRGB'] colorParsley('hsl(112, 22.8%, 66.5%, 0.8)'); // [155, 189, 150, 0.8, true, 'sRGB'] colorParsley('hwb(194, 0%, 0%)'); // converted to sRGB array ``` -------------------------------- ### Initialize Color Parsing Source: https://github.com/myndex/colorparsley/blob/master/docs/versionHistory.md Use the colorParsley function to parse a color string. The output format may vary based on the version and input type. ```javascript let rgbaArray = colorParsley('colorString') ` ``` -------------------------------- ### HTML Input for Real-time Color Entry Source: https://github.com/myndex/colorparsley/blob/master/README.md This HTML input element is configured to use the `entryKeys` JavaScript function for real-time color parsing and display updates as the user types. The `onclick` event selects all text on focus, improving usability. ```html ``` -------------------------------- ### Handle Invalid Color Inputs with colorParsley Source: https://context7.com/myndex/colorparsley/llms.txt Illustrates how colorParsley handles various invalid color inputs, returning specific error codes. ```javascript colorParsley('hello'); // [0, 0, 0, 0, false, 'parsleyError'] colorParsley('rgb(123, 255.7, 123)'); // [0, 0, 0, 0, false, 'parsleyError'] (out of range) colorParsley('rgb(123, 88%, 123)'); // [0, 0, 0, 0, false, 'parsleyError'] (mixed types) colorParsley({ fred: 1, bob: 20 }); // [0, 0, 0, 0, false, 'inputError'] (missing r/g/b) ``` -------------------------------- ### colorToHex Source: https://context7.com/myndex/colorparsley/llms.txt Converts a parsed RGBA array back into a compact hex string. Supports 3-character shorthand and omits alpha when it equals 1. ```APIDOC ## `colorToHex(rgba, allow3)` — RGBA Array to Hex String Converts a parsed RGBA array back into a compact hex string (without `#` prefix). If all channel values are divisible by 17 (i.e., `aa`, `bb`, etc.) and `allow3` is `true` (default), a 3-character shorthand is returned. Alpha is omitted when it equals `1` or is empty. RGB values are rounded before conversion. ### Parameters - **rgba** (Arraycolor 0: ' + color0 + ' \t array: \[' + colorParsley(color0) + '\]' + '
color 1: ' + color1 + ' \t array: \[' + colorParsley(color1) + '\]' + '
color 2: ' + color2 + ' \t array: \[' + colorParsley(color2) + '\]' + '
color 3: ' + color3 + '\t array: \[' + colorParsley(color3) + '\]' + '
color 4: ' + color4 + '(0x808080) \t array: \[' + colorParsley(color4) + '\]' + '
color 5: ' + color5 + ' \t array: \[' + colorParsley(color5) + '\]' + '
color 6: ' + color6 + ' \t array: \[' + colorParsley(color6) + '\]' + '
color 7: ' + color7 + ' \t array: \[' + colorParsley(color7) + '\]' + '
color 8: ' + '#fa' + ' \t array: \[' + colorParsley('#fa') + '\]' + '
color 9: ' + '60%' + ' \t array: \[' + colorParsley('60%') + '\]' + '
color 9b: ' + 'rgb(77%,87%,46%)' + ' \t array: \[' + colorParsley('rgb(77%,87%,46%)') + '\]' + '
color 9c: ' + 'deeppink' + ' \t array: \[' + colorParsley('deeppink') + '\]' + '
color 9cc: ' + '#ff1493' + ' \t array: \[' + colorParsley('#ff1493') + '\]' + '
color10:
' + color10 + ' array: \[' + colorParsley(color10) + '\]' + '
colorObj: ' + colorObject + ' \t array: \[' + colorParsley(colorObject) + '\]' + '
ToHex: ' + color3 + '\t string: ' + colorToHex(colorParsley(color3)) + '
ToHex: ' + 'ABC' + ' \t string: ' + colorToHex(colorParsley('abc')) + '
ToHEX: ' + color10 + ' string: ' + colorToHex(colorParsley(color10)) + '
ToHEX: ' + color11 + ' \t string: ' + colorToHex(colorParsley(color11)) + '
ToHEX: ' + color12 + ' \t string: ' + colorToHex(colorParsley(color12)) + '
ToHEX: ' + color14 + ' \t string: ' + colorToHex(colorParsley(color14)) + '
ToRGB: ' + color10 + ' string: ' + colorToRGB(colorParsley(color10)) + '
ToRGB: ' + color11 + ' \t string: ' + colorToRGB(colorParsley(color11)) + '
ToRGB: ' + color4 + ' \t string: ' + colorToRGB(colorParsley(color4)) + '
ToRGB: ' + 'abc' + ' \t string: ' + colorToRGB(colorParsley('abc')) + '
Fail: ' + 'rgb(123,88%,79)' + ' \t array: \[' + colorParsley('rgb(123,88%,79)') + '\]' + '
Fail: ' + 'hello' + ' \t array: \[' + colorParsley('hello') + '\]' + '
Fail: ' + '{fred: 1, bob: 20, grace: 9,}' + '\t array: \[' + colorParsley({fred: 1, bob: 20, alice: 9,}) + '\]' ; ``` -------------------------------- ### Color Parsing Regex Source: https://github.com/myndex/colorparsley/blob/master/docs/ColorParsleyRegexChart.html This regex handles hex strings (3-6 digits, with optional alpha), RGB(A) values (numbers or percentages), CSS4 color tags (like sRGB, P3), and other color functions (HSL, HWB, LCH) with optional alpha. It includes validation for number ranges and percentage formats. ```regex /(?:^(?:#|0x|) (?:(?:([\da-f])([\da-f])([\da-f])([\da-f])?)(?!\S)| (?:([\da-f]{2})(?:([\da-f]{2})([\da-f]{2})([\da-f]{2})?)?))| (?:(?:^(?:rgba?|)\( ? (?:(?:(?:(255|(?:25[0-4]|2[0-4]\d|1?\d{1,2})(?:\.\d{1,24})?)))(?:,[^\S]?$| (?:(?:, ?| )(255|(?:25[0-4]|2[0-4]\d|1?\d{1,2})(?:\.\d{1,24})?) (?:, ?| )(255|(?:25[0-4]|2[0-4]\d|1?\d{1,2})(?:\.\d{1,24})?)))| (100%|\d{1,2}(?:\.\d{1,24})?%)(?:,?[^\S]?$|(?:(?:, ?| ) ((?:100%|\d{1,2}(?:\.\d{1,24})?%)(?:, ?| ) ((?:100%|\d{1,2}(?:\.\d{1,24})?%)))))| (^(?:color\((srgb|srgb-linear|display-p3|a98-rgb|prophoto-rgb|rec2020|xyz|xyz-d50|xyz-d65) (?: (100%|\d{1,2}(?:\.\d{1,24})?%|[0 ]\.\d{1,24}|[01])) (?: (100%|\d{1,2}(?:\.\d{1,24})?%|[0 ]\.\d{1,24}|[01])) (?: (100%|\d{1,2}(?:\.\d{1,24})?%|[0 ]\.\d{1,24}|[01])))| (^(?:((?:r(?!gb)|c(?!olor)|[abd-qs-z])[a-z]{2,5})\( ? ((?:\d{0,3}\.|)\d{1,24}%?)(?:, ?| ) ((?:\d{0,3}\.|)\d{1,24}%?)(?:, ?| ) ((?:\d{0,3}\.|)\d{1,24}%?)))) (?:(?:,| \/| ) ? (?:(100%|\d{1,2}(?:\.\d{1,24})?%|[0 ]\.\d{1,24}|[01])))? (?:\)| |))[^\S]?$/ ``` -------------------------------- ### JavaScript hwbToRgb Reference Function Source: https://github.com/myndex/colorparsley/blob/master/README.md Reference implementation of hwbToRgb from CSS4. Converts HWB color values to RGB. Hue is expected in degrees, white and black in percentage (0-100). This function utilizes hslToRgb internally. Returns an array of RGB values between 0.0 and 1.0. ```javascript function hwbToRgb(hue, white, black) { // CSS4 reference implementation white /= 100.0; black /= 100.0; if (white + black >= 1) { let gray = white / (white + black); return [gray, gray, gray]; } let rgb = hslToRgb(hue, 100.0, 50.0); for (let i = 0; i < 3; i++) { rgb[i] *= (1 - white - black); rgb[i] += white; } return rgb; // returns 0.0 - 1.0 }; ``` -------------------------------- ### Parse Color String to RGBA Array Source: https://github.com/myndex/colorparsley/blob/master/README.md Use colorParsley() to convert color strings into a standardized RGBA integer array. The output array includes validity and colorspace information. Handles HEX, RGB, and named colors. ```javascript let textColor = colorParsley('#111111'); let backgroundColor = colorParsley('rgb(123,23,233,1.0)'); console.log(textColor); // [17,17,17,1,true,'sRGB'] console.log(backgroundColor); // [123,23,233,1,true,'sRGB'] // STRUCTURE returnedArray = [R,G,B,A,isValidBool,colorspaceString] ``` -------------------------------- ### JavaScript hslToRgb Reference Function Source: https://github.com/myndex/colorparsley/blob/master/README.md Reference implementation of hslToRgb from CSS4. Converts HSL color values to RGB. Hue is expected in degrees (0-360), saturation and lightness in percentage (0-100). Returns an array of RGB values between 0.0 and 1.0. ```javascript function hslToRgb (hue, sat, light) { hue = hue % 360; if (hue < 0) { hue += 360; } sat /= 100.0; light /= 100.0; function f(n) { let k = (n + hue/30) % 12; let a = sat * Math.min(light, 1 - light); return light - a * Math.max(-1, Math.min(k - 3, 9 - k, 1)); } return [f(0), f(8), f(4)]; // returns 0.0 - 1.0 }; ``` -------------------------------- ### ColorParsley Regex String Source: https://github.com/myndex/colorparsley/blob/master/docs/ColorParsleyRegexChart.html This is the primary regular expression used by colorParsley for parsing color values. It supports hex, rgb, rgba, and CSS color() function formats. ```regex // colorParsley regex string 0.1.6 /(?:^(?:#|0x|)(?:(?:([\da-f])([\da-f])([\da-f])([\da-f])?)(?!\S)|(?:([\da-f]{2})(?:([\da-f]{2})([\da-f]{2})([\da-f]{2})?)?))|(?:(?:^(?:rgba?|)\( ?(?:(?:(?:(255|(?:25[0-4]|2[0-4]\d|1?\d{1,2})(?:\.\d{1,24})?)))(?:,[^\S]?$|(?:(?:, ?| )(255|(?:25[0-4]|2[0-4]\d|1?\d{1,2})(?:\.\d{1,24})?)(?:, ?| )(255|(?:25[0-4]|2[0-4]\d|1?\d{1,2})(?:\.\d{1,24})?)))|(100%|\d{1,2}(?:\.\d{1,24})?%)(?:,?[^\S]?$|(?:(?:, ?| )(?:(100%|\d{1,2}(?:\.\d{1,24})?%)(?:, ?| )(100%|\d{1,2}(?:\.\d{1,24})?%)))))|^(?:color\((srgb|srgb-linear|display-p3|a98-rgb|prophoto-rgb|rec2020|xyz|xyz-d50|xyz-d65) (?:(100%|\d{1,2}(?:\.\d{1,24})?%|[0 ]\.\d{1,24}|[01])) (?:(100%|\d{1,2}(?:\.\d{1,24})?%|[0 ]\.\d{1,24}|[01])) (?:(100%|\d{1,2}(?:\.\d{1,24})?%|[0 ]\.\d{1,24}|[01])))|^(?:((?:r(?!gb)|c(?!olor)|[abd-qs-z])[a-z]{2,5})\( ?((?:\d{0,3}\.|)\d{1,24}%?)(?:, ?| )((?:\d{0,3}\.|)\d{1,24}%?)(?:, ?| )((?:\d{0,3}\.|)\d{1,24}%?))))(?:(?:,| \/| ) ?(?:(100%|\d{1,2}(?:\.\d{1,24})?%|0?\.\d{1,24}|[01])))?(?:\)| |))[^\S]?$/ ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.