Search This Blog

Monday, August 13, 2012

Change terminal background randomly

From time to time I like to change my terminal background color. I randomly came across this post. I think that was a nice implementation, but I personally prefer somewhat dark colors for by background, so I decided to edit the proposed script to randomize the colors in the HSV color space, to have easy control over the "darkness" of the color. The result if the following applescript:
-- © Copyright 2006, Red Sweater Software. All Rights Reserved.
-- Permission to copy granted for personal use only. All copies of this script
-- must retain this copyright information and all lines of comments below, up to
-- and including the line indicating "End of Red Sweater Comments". 
--
-- Any commercial distribution of this code must be licensed from the Copyright
-- owner, Red Sweater Software.
--
-- This script alters the color of the frontmost Terminal window to be something 
-- random.
--
-- End of Red Sweater Comments

(*
Script obtained from http://www.red-sweater.com/blog/220/random-color-terminal

Modified to use HSV instead of RGB colorspace. 
This way is possible to keep the randomized background color to 
a "dark" (low V or "value" HSV), which I think is more pleasing.
August 12, 2012
australsounds.com
*)


-- This nasty constant might as well be a global
global kColorValueMax
set kColorValueMax to 65535

-- define a V range ("value" in the HSV color space)
set {kVmin, kVmax} to {0.2, 0.4}

-- Choose a random HSV color for the background
set randomHue to (random number) * 360
set randomSaturation to (random number)
set randomValue to kVmin + (random number) * (kVmax - kVmin)
-- here is where I specify I like dark colors (but not too dark)

-- Map HSV color to RGB (from http://www.cs.rit.edu/~ncs/color/t_convert.html)
if randomSaturation = 0 then
 set randomRed to randomValue
 set randomGreen to randomValue
 set randomBlue to randomValue
else
 set h to randomHue / 60
 set i to floor(h)
 set f to h - i
 set p to randomValue * (1 - randomSaturation)
 set q to randomValue * (1 - randomSaturation * f)
 set t to randomValue * (1 - randomSaturation * (1 - f))
 if i = 0 then
  set randomRed to randomValue
  set randomGreen to t
  set randomBlue to p
 else if i = 1 then
  set randomRed to q
  set randomGreen to randomValue
  set randomBlue to p
 else if i = 2 then
  set randomRed to p
  set randomGreen to randomValue
  set randomBlue to t
 else if i = 3 then
  set randomRed to p
  set randomGreen to q
  set randomBlue to randomValue
 else if i = 4 then
  set randomRed to t
  set randomGreen to p
  set randomBlue to randomValue
 else
  set randomRed to randomValue
  set randomGreen to p
  set randomBlue to q
 end if
end if

-- now take the RGB in the [0,1] range to the [0, kColorValueMax]
set randomRed to randomRed * kColorValueMax
set randomGreen to randomGreen * kColorValueMax
set randomBlue to randomBlue * kColorValueMax

-- helper function
on floor(x)
 if x > 0 then
  set y to x div 1
 else if x < 0 then
  set y to -(1 - x) div 1
 else
  set y to 0
 end if
 return y
end floor

-- Finally, set the RGB color for the background
set myBackgroundColor to {randomRed, randomGreen, randomBlue}

-- Select appropriate text colors based on that background
set {myTextColor, myBoldColor} to my ContrastingTextColors(myBackgroundColor)

-- Now inflict them on the frontmost window
tell application "Terminal"
 set targetWindow to window 1
 set background color of targetWindow to myBackgroundColor
 set cursor color of targetWindow to myTextColor
 set normal text color of targetWindow to myTextColor
 set bold text color of targetWindow to myBoldColor
end tell

on ContrastingTextColors(myColor)
 set whiteColor to {kColorValueMax, kColorValueMax, kColorValueMax, kColorValueMax}
 set lightGreyColor to {40000, 40000, 40000, kColorValueMax}
 set blackColor to {0, 0, 0, kColorValueMax}
 set darkGreyColor to {20000, 20000, 20000, kColorValueMax}
 
 -- From http://www.wilsonmar.com/1colors.htm
 set myRed to (item 1 of myColor) / kColorValueMax
 set myGreen to (item 2 of myColor) / kColorValueMax
 set myBlue to (item 3 of myColor) / kColorValueMax
 set magicY to (0.3 * myRed) + (0.59 * myGreen) + (0.11 * myBlue)
 if (magicY < 0.5) then
  return {whiteColor, lightGreyColor}
 else
  return {blackColor, darkGreyColor}
 end if
end ContrastingTextColors

Automatic execution

With that in place, I decided to run this script every time I open a new Terminal window. To do that simply add this line to your .bash_profile:
osascript PATH/TO/YOUR/SCRIPT

1 comment:

  1. This is very simple, but so effective! I love the design and I will definitely make one for myself:) Have a nice day
    Ludostar.co Skinscraft.online footballgamesbeast.com

    ReplyDelete