|
80 | 80 | {
|
81 | 81 | "name": "Color Manipulation",
|
82 | 82 | "snippets": [
|
| 83 | + { |
| 84 | + "title": "Hex to RGB Color", |
| 85 | + "description": "Converts hexadecimal color code to RGB color values.", |
| 86 | + "author": "pvictordev", |
| 87 | + "tags": [ |
| 88 | + "color", |
| 89 | + "conversion" |
| 90 | + ], |
| 91 | + "contributors": [], |
| 92 | + "code": "function hexToRgb(hex) {\n let sanitizedHex = hex.startsWith(\"#\") ? hex.slice(1) : hex;\n\n if (sanitizedHex.length === 3) {\n sanitizedHex = [...sanitizedHex].map((char) => char + char).join(\"\");\n }\n\n const bigint = parseInt(sanitizedHex, 16);\n\n return {\n r: (bigint >> 16) & 0xff, \n g: (bigint >> 8) & 0xff, \n b: bigint & 0xff, \n };\n}\n\n// Usage:\nconsole.log(hexToRgb(\"#ff5733\")); // { r: 255, g: 87, b: 51 }\nconsole.log(hexToRgb(\"#ffff\")); // { r: 0, g: 255, b: 255 }\n" |
| 93 | + }, |
| 94 | + { |
| 95 | + "title": "HSL to RGB Color", |
| 96 | + "description": "Converts HSL color values to RGB color values.", |
| 97 | + "author": "pvictordev", |
| 98 | + "tags": [ |
| 99 | + "color", |
| 100 | + "conversion" |
| 101 | + ], |
| 102 | + "contributors": [], |
| 103 | + "code": "function hslToRgb(h, s, l) {\n s /= 100;\n l /= 100;\n const c = (1 - Math.abs(2 * l - 1)) * s;\n const x = c * (1 - Math.abs((h / 60) % 2 - 1));\n const m = l - c / 2;\n\n const [r, g, b] = \n h < 60 ? [c, x, 0] :\n h < 120 ? [x, c, 0] :\n h < 180 ? [0, c, x] :\n h < 240 ? [0, x, c] :\n h < 300 ? [x, 0, c] :\n [c, 0, x];\n\n return {\n r: Math.round((r + m) * 255),\n g: Math.round((g + m) * 255),\n b: Math.round((b + m) * 255),\n };\n}\n\n// Usage:\nconsole.log(hslToRgb(14, 100, 60)); // { r: 255, g: 87, b: 51 }\nconsole.log(hslToRgb(0, 0, 100)); // { r: 255, g: 255, b: 255 }\n" |
| 104 | + }, |
83 | 105 | {
|
84 | 106 | "title": "RGB to Hex Color",
|
85 | 107 | "description": "Converts RGB color values to hexadecimal color code.",
|
|
90 | 112 | ],
|
91 | 113 | "contributors": [],
|
92 | 114 | "code": "function rgbToHex(r, g, b) {\n const toHex = (n) => {\n const hex = n.toString(16);\n return hex.length === 1 ? '0' + hex : hex;\n };\n \n return '#' + toHex(r) + toHex(g) + toHex(b);\n}\n\n// Usage:\nconsole.log(rgbToHex(255, 128, 0)); // Output: \"#ff8000\"\nconsole.log(rgbToHex(0, 255, 0)); // Output: \"#00ff00\"\n"
|
| 115 | + }, |
| 116 | + { |
| 117 | + "title": "RGB to HSL Color", |
| 118 | + "description": "Converts RGB color values to HSL color values.", |
| 119 | + "author": "pvictordev", |
| 120 | + "tags": [ |
| 121 | + "color", |
| 122 | + "conversion" |
| 123 | + ], |
| 124 | + "contributors": [], |
| 125 | + "code": "function rgbToHsl(r, g, b) {\n [r, g, b] = [r, g, b].map((v) => v / 255);\n\n const max = Math.max(r, g, b);\n const min = Math.min(r, g, b);\n const delta = max - min;\n\n const l = (max + min) / 2;\n\n if (delta === 0) return { h: 0, s: 0, l: Math.round(l * 100) };\n\n const s = l > 0.5 ? delta / (2 - max - min) : delta / (max + min);\n\n const h = \n max === r ? ((g - b) / delta + (g < b ? 6 : 0)) :\n max === g ? (b - r) / delta + 2 :\n (r - g) / delta + 4;\n\n return {\n h: Math.round(h * 60), \n s: Math.round(s * 100),\n l: Math.round(l * 100), \n };\n}\n\n// Usage:\nconsole.log(rgbToHsl(255, 87, 51)); // { h: 14, s: 100, l: 60 }\nconsole.log(rgbToHsl(255, 255, 255)); // { h: 0, s: 0, l: 100 }\n" |
93 | 126 | }
|
94 | 127 | ]
|
95 | 128 | },
|
|
0 commit comments