| 73 | } |
| 74 | |
| 75 | const handler = (data: Buffer) => { |
| 76 | const str = data.toString() |
| 77 | const match = str.match(/\x1b]11;([^\x07\x1b]+)/) |
| 78 | if (match) { |
| 79 | cleanup() |
| 80 | const color = match[1] |
| 81 | // Parse RGB values from color string |
| 82 | // Formats: rgb:RR/GG/BB or #RRGGBB or rgb(R,G,B) |
| 83 | let r = 0, |
| 84 | g = 0, |
| 85 | b = 0 |
| 86 | |
| 87 | if (color.startsWith("rgb:")) { |
| 88 | const parts = color.substring(4).split("/") |
| 89 | r = parseInt(parts[0], 16) >> 8 // Convert 16-bit to 8-bit |
| 90 | g = parseInt(parts[1], 16) >> 8 // Convert 16-bit to 8-bit |
| 91 | b = parseInt(parts[2], 16) >> 8 // Convert 16-bit to 8-bit |
| 92 | } else if (color.startsWith("#")) { |
| 93 | r = parseInt(color.substring(1, 3), 16) |
| 94 | g = parseInt(color.substring(3, 5), 16) |
| 95 | b = parseInt(color.substring(5, 7), 16) |
| 96 | } else if (color.startsWith("rgb(")) { |
| 97 | const parts = color.substring(4, color.length - 1).split(",") |
| 98 | r = parseInt(parts[0]) |
| 99 | g = parseInt(parts[1]) |
| 100 | b = parseInt(parts[2]) |
| 101 | } |
| 102 | |
| 103 | // Calculate luminance using relative luminance formula |
| 104 | const luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255 |
| 105 | |
| 106 | // Determine if dark or light based on luminance threshold |
| 107 | resolve(luminance > 0.5 ? "light" : "dark") |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | process.stdin.setRawMode(true) |
| 112 | process.stdin.on("data", handler) |