Skip to content

Instantly share code, notes, and snippets.

@denisdefreyne
Created November 10, 2022 12:06
Show Gist options
  • Save denisdefreyne/0e6e8f439e38b7b67dee8f2d3be794a6 to your computer and use it in GitHub Desktop.
Save denisdefreyne/0e6e8f439e38b7b67dee8f2d3be794a6 to your computer and use it in GitHub Desktop.
Calculating terminal background color in Ruby
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