Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
"True transparency" clock.
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
Andersson
Guru
Guru


Joined: 12 Jul 2003
Posts: 525
Location: Göteborg, Sweden

PostPosted: Sun Dec 28, 2003 4:31 am    Post subject: "True transparency" clock. Reply with quote

I'm a little bored right now, so I'll share a trick I use when I want to use all of the screen and still have a clock visible. Or just sometimes when I want a little eyecandy. (Yeah, this is my idea of eyecandy -simple and functional. :P ) Here is a screenshot if you want to know what it looks like. Add the shiny panel, icons and background image of your own desktop and you'll see it fits right in!

True transparancy is a little too much said. The clock does shine through a transparent terminal emulator though, and that's what counts isn't it. This script simply draws a clock on your wallpaper. That way all programs with fake transparency will add it to their background and you'll be able to see your clock all the time. It looks good, and I think it's convenient, but nothing is perfect.

The screen flickers when the background changes. Also, to prevent it from stealing system resources I start it with a nice value of 15 (or is it -15? I never get what is a dash and what is a minus...) which means the clock will lag behind if you do something else that takes a lot of system resources. And aterms don't update their background when you move them or change desktop... But if you just want a nice clock to look at while you code, this might be something for you.

It is a python script, so you need to have python installed. It also needs the date command, but I guess most have that. You also need a program that changes your background. In the script (far down) I use hsetroot. This can be replaced with Esetroot, xsetroot or something else you might already have.

The script also uses imagemagick, a great program that can be used to manipulate images from the command line. A text based image editor! Does it get any better? :D There is a jungle of commands available, the one used in this script draws polygons on a picture. The polygons are the hands of the clock of course, and the shape and size of these are calculated with a little bit of trigonometry that I assume you either know or don't want to bother with.

The rest of the script is self-explanatory I think. Just cut and paste, make it executable with chmod u+x clock.py or whatever file name you might choose. Edit some of the variables in the script, like the path to your wallpaper and a filename to use for the generated image.

So finally, here's the script, and remember, start it with something like nice -15 ~/.clock/clock.py & so it doesn't interrupt you every two minutes.

*edit* download it here instead to avoid the problem with spaces some people describe below.
*edit again* changed hsetroot to Esetroot -more people will have that installed.

Code:
#!/usr/bin/python

######################################################################
## This python script will draw hour and minute hands on a picture. ##
## The purpose is to create a clock on the wallpaper, to enhance    ##
## the illusion of fake transparency. The wallpaper should already  ##
## have the face of the clock drawn, otherwise it'll look silly.    ##
##                                                                  ##
## This script requires date, mogrify (from imagemagick) and        ##
## Esetroot in the PATH. If you use it as an executable, adjust the ##
## path to python on the first line. Yeah, you need python as well. ##
##                                                                  ##
######################################################################

from math import cos, sin, pi
import os
import time

# Needed variables.
infile="~/.clock/wallpaper.png" # The orignal file, to be drawn on.
outfile="~/.clock/wallpaper_clock.png" # Filename to save new picture as.
x0=1100 # Coordinates in pixels, for center of the clock.
y0=159
h_length=50 # Total length of the hour hand, in pixels.
h_length_to_thickest=25 # The distance from origo to the thickest part of the hour hand.
h_thickness_angle=0.22 # The angle from origo between the tip and the thick part of hour hand (in radians).
h_color="#29466e" # The color of the hour hand.

m_length=80 # These four variables same as above, but describing the minute hand.
m_length_to_thickest=40
m_thickness_angle=0.1
m_color="#29466e"

interval=120 # How long to wait between ticks, in seconds.

while True:

   # Now let's find out what time it is.
   hour = int(os.popen('date +%H').read())
   minute = int(os.popen('date +%M').read())

   """
   Now we'll have many variables to keep track of...
   We need 4 coordinate pairs for each hand to draw the polygon. One of these points
   is the center, (x0,y0). The others are called point a, (the tip) and points b
   and c (the two "wide" corners). Variable names on this form:
   h is for the hour hand, m is for the minute hand.
   x,y for the coordinate.
   0,a,b,c for the point they describe.
   ang is the angle. Note, this is the mathematical definition, starting at 3 o'clock
   and moving counter-clockwise, 2 pi units per revolution. Radians that is...
   """

   hang = pi/2 - 2*pi*hour/12.0 - minute*pi / 360.0
   mang = pi/2 - 2*pi*minute/60.0

   hxa = x0 + h_length * cos(hang)
   hya = y0 - h_length * sin(hang) # Note, negative y-coords is because pixels count from top of image.
   hxb = x0 + h_length_to_thickest * cos(hang - h_thickness_angle)
   hyb = y0 - h_length_to_thickest * sin(hang - h_thickness_angle)
   hxc = x0 + h_length_to_thickest * cos(hang + h_thickness_angle)
   hyc = y0 - h_length_to_thickest * sin(hang + h_thickness_angle)

   mxa = x0 + m_length * cos(mang)
   mya = y0 - m_length * sin(mang)
   mxb = x0 + m_length_to_thickest * cos(mang - m_thickness_angle)
   myb = y0 - m_length_to_thickest * sin(mang - m_thickness_angle)
   mxc = x0 + m_length_to_thickest * cos(mang + m_thickness_angle)
   myc = y0 - m_length_to_thickest * sin(mang + m_thickness_angle)

   os.system("cp " + infile + " " + outfile) # We don't want to overwrite the original.

   # Now let's draw the polygons.
   os.system("mogrify -fill \"" + h_color
      + "\" -draw \'polygon "
      + str(int(hxb+0.5)) + "," # The strange format str(int(bla+0.5)) is to get rid of...
      + str(int(hyb+0.5)) + " " # ... decimals and round to nearest integer, then...
      + str(int(hxa+0.5)) + "," # ... convert the integer to a string.
      + str(int(hya+0.5)) + " "
      + str(int(hxc+0.5)) + "," # Note, the order of the points has to follow the edge.
      + str(int(hyc+0.5)) + " " # Otherwise, we'll end up with an hourglass shape.
      + str(x0) + "," + str(y0) + "\' "

   # Here's the minute hand.
      + " -fill \"" + m_color + "\""
      + " -draw \'polygon "
      + str(int(mxb+0.5)) + ","
      + str(int(myb+0.5)) + " "
      + str(int(mxa+0.5)) + ","
      + str(int(mya+0.5)) + " "
      + str(int(mxc+0.5)) + ","
      + str(int(myc+0.5)) + " "
      + str(x0) + "," + str(y0) + "\' "

      + outfile)

   # Last but not least, update the wallpaper.
   os.system("Esetroot -fill " + outfile)
   # This one below might be to prefer if script is run from cron or something...
   # os.system("env DISPLAY=:0.0 Esetroot -fill " + outfile)

   # Now we will wait for a while, then start over again!
   time.sleep(interval)


Last edited by Andersson on Mon Dec 29, 2003 10:56 pm; edited 2 times in total
Back to top
View user's profile Send private message
rojaro
l33t
l33t


Joined: 06 May 2002
Posts: 732

PostPosted: Sun Dec 28, 2003 1:16 pm    Post subject: nice one ... Reply with quote

nice one ... :)
_________________
A mathematician is a machine for turning coffee into theorems. ~ Alfred Renyi (*1921 - †1970)
Back to top
View user's profile Send private message
AgenT
Apprentice
Apprentice


Joined: 18 May 2003
Posts: 280

PostPosted: Sun Dec 28, 2003 4:48 pm    Post subject: Reply with quote

Very nice and original.
Back to top
View user's profile Send private message
ed0n
l33t
l33t


Joined: 23 Apr 2003
Posts: 638
Location: Prishtine/Kosove

PostPosted: Sun Dec 28, 2003 5:30 pm    Post subject: Reply with quote

really good work.
Back to top
View user's profile Send private message
floam
Veteran
Veteran


Joined: 27 Oct 2002
Posts: 1067
Location: Vancouver, WA USA

PostPosted: Sun Dec 28, 2003 7:32 pm    Post subject: Reply with quote

What a nasty hack :-/
_________________
Think about your breathing.
http://floam.sh.nu
Back to top
View user's profile Send private message
anil_et
Tux's lil' helper
Tux's lil' helper


Joined: 22 Jul 2002
Posts: 98

PostPosted: Mon Dec 29, 2003 9:00 am    Post subject: Reply with quote

Hi,
While I execute the script I get the error
Code:

anil@puppy anil $ ./clock.py
  File "./clock.py", line 15
    from math import cos, sin, pi
    ^
SyntaxError: invalid syntax

Help please !!!!!!
Back to top
View user's profile Send private message
Andersson
Guru
Guru


Joined: 12 Jul 2003
Posts: 525
Location: Göteborg, Sweden

PostPosted: Mon Dec 29, 2003 4:19 pm    Post subject: Reply with quote

anil_et wrote:
Hi,
While I execute the script I get the error
Code:

anil@puppy anil $ ./clock.py
  File "./clock.py", line 15
    from math import cos, sin, pi
    ^
SyntaxError: invalid syntax

Help please !!!!!!

Perhaps import math works better -what version of python do you use? I think it's supposed to be just import math, but that gave me a syntax error...
*edit* perhaps from math import * ?
Back to top
View user's profile Send private message
Niek
Apprentice
Apprentice


Joined: 14 May 2002
Posts: 236
Location: Houten, The Netherlands

PostPosted: Mon Dec 29, 2003 6:03 pm    Post subject: Reply with quote

anil_et wrote:
Hi,
While I execute the script I get the error
Code:

anil@puppy anil $ ./clock.py
  File "./clock.py", line 15
    from math import cos, sin, pi
    ^
SyntaxError: invalid syntax

Help please !!!!!!
Same problem here. It's because on the beginning of every line a space is added (copy/paste error). Just remove the space on the beginning of every line and it will work.
_________________
Bezoek ook eens de Nederlandstalige Gentoo IRC channel op irc.freenode.net - #gentoo-nl
Back to top
View user's profile Send private message
MighMoS
Guru
Guru


Joined: 24 Apr 2003
Posts: 416
Location: @ ~

PostPosted: Mon Dec 29, 2003 7:09 pm    Post subject: Reply with quote

I get hsetroot: command not found :-(
Back to top
View user's profile Send private message
vdboor
Guru
Guru


Joined: 03 Dec 2003
Posts: 592
Location: The Netherlands

PostPosted: Mon Dec 29, 2003 8:09 pm    Post subject: Reply with quote

Does this program also work with the kde desktop panel?
_________________
The best way to accelerate a windows server is by 9.81M/S²
Linux user #311670 and Yet Another Perl Programmer

[ screenies | Coding on KMess ]
Back to top
View user's profile Send private message
Andersson
Guru
Guru


Joined: 12 Jul 2003
Posts: 525
Location: Göteborg, Sweden

PostPosted: Mon Dec 29, 2003 10:45 pm    Post subject: Reply with quote

MighMoS wrote:
I get hsetroot: command not found :-(

You can find hsetroot here: http://thegraveyard.org/hsetroot.php -but you probably have another program installed already that does the same thing. Check for Esetroot, i think that one is more common.

vdboor wrote:
Does this program also work with the kde desktop panel?

I have no idea what that is... sorry. But all the script does is change the background image -if you can normally do that, then it should work :)
Back to top
View user's profile Send private message
Stu L Tissimus
Veteran
Veteran


Joined: 08 Jun 2003
Posts: 1339
Location: NJ, 5 minutes from NYC

PostPosted: Wed Feb 11, 2004 3:00 am    Post subject: Reply with quote

Wow.... O_O

Nice job!

(IMO, you should put that in the Kahakai wiki... Not exactly related, but it's in pytohnand it has to do with window managers... And you're a Kahakai user... :) )
_________________
old outdated sig
Back to top
View user's profile Send private message
Andersson
Guru
Guru


Joined: 12 Jul 2003
Posts: 525
Location: Göteborg, Sweden

PostPosted: Wed Feb 11, 2004 1:00 pm    Post subject: Reply with quote

Stu L Tissimus wrote:
(IMO, you should put that in the Kahakai wiki... Not exactly related, but it's in pytohnand it has to do with window managers... And you're a Kahakai user... :) )

Nah, it is a nasty hack. I don't use it anymore and I'd surprised if anyone kept it for long after trying it. It shows the wrong time too often, and that is unacceptable for a clock.

It was fun to write though. I've been thinking of a better version that would work with all programs, not just "transparent" ones. It would have the clock in a borderless window on top of other programs instead of in the root. I'd just need some way to get a screenshot of the desktop below it and a way to avoid mouse/keyboard focus...

But I don't have the time... besides there probably is a program like that somewhere on sourceforge or freshmeat already ;)
Back to top
View user's profile Send private message
axxackall
l33t
l33t


Joined: 06 Nov 2002
Posts: 651
Location: Toronto, Ontario, 3rd Rock From Sun

PostPosted: Thu Feb 12, 2004 11:38 pm    Post subject: Reply with quote

Anyone tried it with Gnome?
_________________
"Lisp is a programmable programming language." - John Foderaro, CACM, September 1991
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum