Created
November 10, 2022 12:06
-
-
Save denisdefreyne/0e6e8f439e38b7b67dee8f2d3be794a6 to your computer and use it in GitHub Desktop.
Calculating terminal background color in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'io/console' | |
# Query RGB background color | |
raw = STDIN.raw do |stdin| | |
print "\e]11;?\a" | |
stdin.read(25) | |
end | |
# Parse | |
r, g, b = raw.scan(/[0-9a-f]{4}/).map { |part| part.to_i(16) / 65_535.0 } | |
# Calculate brightness | |
brightness = Math.sqrt(((0.299 * (r**2)) + (0.587 * (g**2)) + (0.114 * (b**2)))) | |
puts "Brightness: #{format '%.2f', brightness}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment