Skip to content

Instantly share code, notes, and snippets.

View Louis-7's full-sized avatar

Louis Louis-7

  • 09:12 (UTC +08:00)
View GitHub Profile
@Louis-7
Louis-7 / background-color.js
Last active April 1, 2025 01:04
Determine font color based on the background color
// suppose background color is: rbg(0, 23, 255)
function textColorBasedOnBackground(backgroundColor) {
backgroundColor = backgroundColor.substring(1);
const r = parseInt(backgroundColor.substring(0,2), 16); // 0 ~ 255
const g = parseInt(backgroundColor.substring(2,4), 16);
const b = parseInt(backgroundColor.substring(4,6), 16);
const srgb = [r / 255, g / 255, b / 255];
const x = srgb.map((i) => {
if (i <= 0.04045) {