Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


my script | 37 comments | Create New Account
Click here to return to the 'my script' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
my script
Authored by: tim1724 on Aug 27, '04 06:34:45PM

I use a Perl script which chooses colors based on the HSV system. I have it set to choose pastel colors, but you can configure it easily if you have other preferences.

  • Hue is the color (red,green,orange,purple,etc. ranges from 0 to 360)
  • Saturation is how strong the color is (0=grey, 1=most saturated)
  • Value is the brightness (0=black, 1=full brightness)
  • Alpha is the transparency (0=completely transparent, 1=completely opaque)

#!/usr/bin/perl


# parameters which determine the range of random colors chosen
my  ($min_h,$max_h) =  ( 0,    360   );   # hue        (0-360)
my  ($min_s,$max_s) =  ( 0.30, 0.60  );   # saturation (0-1)
my  ($min_v,$max_v) =  ( 0.75, 1.00  );   # brightness (0-1)
my  ($min_a,$max_a) =  ( 0.65, 0.85  );   # alpha      (0-1)


# generate random number between two values
sub randbetween {
    my ($min,$max) = @_;

    return $min + rand($max-$min);
}



# HSV to RGB conversion algorithm from  http://www.cs.rit.edu/~ncs/color/t_convert.html

sub hsv2rgb {
    my ($h,$s,$v) = @_;

    # handle greyscale case
    return ($v,$v,$v) if ($s == 0);

    my ($i, $f, $p, $q, $t);

    $h /= 60;  # convert to sector between 0 and 5
    $i = int($h);
    $f = $h - $i;
    $p = $v * (1-$s);
    $q = $v * (1-$s*$f);
    $t = $v * (1-$s*(1-$f));

    return ($v, $t, $p) if $i == 0;
    return ($q, $v, $p) if $i == 1;
    return ($p, $v, $t) if $i == 2;
    return ($p, $q, $v) if $i == 3;
    return ($t, $p, $v) if $i == 4;
    return ($v, $p, $q);
}



# turn list of floats into a string suitable for applescript
sub rgb2string {
    return join ", ", map(int(65536*$_),@_);
}




#main program

my @rgb = hsv2rgb(randbetween($min_h,$max_h), randbetween($min_s,$max_s), randbetween($min_v,$max_v));
my $alpha = int(65536*randbetween($min_a, $max_a));
my $color = '{ ' . rgb2string(@rgb). ", $alpha }";

system "osascript", "-e", "tell application \"Terminal\" to set the background color of window 1 to $color";

You could easily have this randomize the text color or cursor color as well, but I prefer to leave them set to black.

---
Tim Buchheim

[ Reply to This | # ]

my script
Authored by: mysty on Sep 10, '04 03:48:13PM
Again (as described in reply to zeorge's post above, 10.3 & bash) I get the following error for a new Terminal window:

-bash: /Users/me/scripts/termcolours2.pl: line 5: syntax error near unexpected token `$min_h,$max_h'
-bash: /Users/me/scripts/termcolours2.pl: line 5: `my  ($min_h,$max_h) =  ( 0,    360   );   # hue        (0-360)'
is it my perl all skew-wiffied?


Characteristics of this binary (from libperl): 
  Compile-time options: MULTIPLICITY USE_ITHREADS USE_LARGE_FILES PERL_IMPLICIT_CONTEXT
  Locally applied patches:
        RC3
  Built under darwin
  Compiled at Sep 12 2003 19:50:49
  %ENV:
    PERL5LIB="/sw/lib/perl5"
  @INC:
    /sw/lib/perl5
    /System/Library/Perl/5.8.1/darwin-thread-multi-2level
    /System/Library/Perl/5.8.1
    /Library/Perl/5.8.1/darwin-thread-multi-2level
    /Library/Perl/5.8.1
    /Library/Perl
    /Network/Library/Perl/5.8.1/darwin-thread-multi-2level
    /Network/Library/Perl/5.8.1
    /Network/Library/Perl
TIA and for the script

[ Reply to This | # ]
my script
Authored by: tim1724 on Sep 14, '04 03:11:51PM

My guess is that copying & pasting the script messed it up somehow.

Try downloading it from my website.

---
Tim Buchheim

[ Reply to This | # ]

my script
Authored by: sweyhrich on Oct 14, '04 06:40:27PM

What exactly is the command to put in .bashrc in order to execute the perl script? I've tried various things, and each time I open a new terminal window, I still get the same colors as it was before.

---
Steven Weyhrich
http://apple2history.org



[ Reply to This | # ]
my script
Authored by: mysty on Sep 10, '04 03:59:16PM
maybe the fact that the

. ~/scripts/termcolours2.pl
line only gets called from the .profile (and not from the .bashrc) is a clue? You can see I am way in the dark

[ Reply to This | # ]