File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change 820820 " performance"
821821 ],
822822 "author" : " dostonnabotov"
823+ },
824+ {
825+ "title" : " Get Contrast Color" ,
826+ "description" : " Returns either black or white text color based on the brightness of the provided hex color." ,
827+ "code" : [
828+ " const getContrastColor = (hexColor) => {" ,
829+ " // Expand short hex color to full format" ,
830+ " if (hexColor.length === 4) {" ,
831+ " hexColor = `#${hexColor[1]}${hexColor[1]}${hexColor[2]}${hexColor[2]}${hexColor[3]}${hexColor[3]}`;" ,
832+ " }" ,
833+ " const r = parseInt(hexColor.slice(1, 3), 16);" ,
834+ " const g = parseInt(hexColor.slice(3, 5), 16);" ,
835+ " const b = parseInt(hexColor.slice(5, 7), 16);" ,
836+ " const brightness = (r * 299 + g * 587 + b * 114) / 1000;" ,
837+ " return brightness >= 128 ? \" #000000\" : \" #FFFFFF\" ;" ,
838+ " };" ,
839+ " " ,
840+ " // Usage:" ,
841+ " console.log(getContrastColor('#fff')); // Output: #000000 (black)" ,
842+ " console.log(getContrastColor('#123456')); // Output: #FFFFFF (white)" ,
843+ " console.log(getContrastColor('#ff6347')); // Output: #000000 (black)" ,
844+ " console.log(getContrastColor('#f4f')); // Output: #000000 (black)"
845+ ],
846+ "tags" : [
847+ " color" ,
848+ " hex" ,
849+ " contrast" ,
850+ " brightness" ,
851+ " utility"
852+ ],
853+ "author" : " yaya12085"
823854 }
824855 ]
825856 },
You can’t perform that action at this time.
0 commit comments