Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion public/data/javascript.json
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,33 @@
],
"tags": ["javascript", "utility", "throttle", "performance"],
"author": "dostonnabotov"
}
},
{
"title": "Get Contrast Color",
"description": "Returns either black or white text color based on the brightness of the provided hex color.",
"code": [
"const getContrastColor = (hexColor) => {",
" // Expand short hex color to full format",
" if (hexColor.length === 4) {",
" hexColor = `#${hexColor[1]}${hexColor[1]}${hexColor[2]}${hexColor[2]}${hexColor[3]}${hexColor[3]}`;",
" }",
" const r = parseInt(hexColor.slice(1, 3), 16);",
" const g = parseInt(hexColor.slice(3, 5), 16);",
" const b = parseInt(hexColor.slice(5, 7), 16);",
" const brightness = (r * 299 + g * 587 + b * 114) / 1000;",
" return brightness >= 128 ? \"#000000\" : \"#FFFFFF\";",
"};",
"",
"// Usage:",
"console.log(getContrastColor('#fff')); // Output: #000000 (black)",
"console.log(getContrastColor('#123456')); // Output: #FFFFFF (white)",
"console.log(getContrastColor('#ff6347')); // Output: #000000 (black)",
"console.log(getContrastColor('#f4f')); // Output: #000000 (black)"
],
"tags": ["color", "hex", "contrast", "brightness", "utility"],
"author": "yaya12085"
}

]
},
{
Expand Down