[Solved]Mix-n-Match clock.lua codes

Add functionality to your desktop
Forum rules
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
User avatar
mzsade
Level 5
Level 5
Posts: 776
Joined: Sun Jul 19, 2009 4:36 am

[Solved]Mix-n-Match clock.lua codes

Post by mzsade »

I tried renaming the variables, unfortunately i am not even good enough to be called a script kiddie.and made a right mess of it. Let me explain,
This is Mr.Peachy's clock,
scrot1.png

Code: Select all

--[[drawing a clock
in conkyrc

lua_load /path/clock.lua
lua_draw_hook_pre main
TEXT


]]

require 'cairo'

function conky_draw_clock()
if conky_window == nil then return end
local cs = cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, conky_window.width, conky_window.height)
cr = cairo_create(cs)
--#########################################################################################################
--CLOCK SETTINGS
clock_radius=54
clock_centerx=167
clock_centery=175
--set border options
clock_border_width=12
--set color and alpha for clock border
cbr,cbg,cbb,cba=0,1,0,0.2--full opaque white
--gap from clock border to hour marks
b_to_m=-2
--set mark length
m_length=6
--set mark line width
m_width=3
--set mark line cap type
m_cap=CAIRO_LINE_CAP_ROUND
--set mark color and alpha,red blue green alpha
mr,mg,mb,ma=0,1,0,1--opaque white
--seconds hand setup
--set length of seconds hand
sh_length=47
--set hand width
sh_width=1
--set hand line cap
sh_cap=CAIRO_LINE_CAP_ROUND
--set seconds hand color
shr,shg,shb,sha=1,0,0,1--fully opaque red
--minues hand setup
--set length of minutes hand
mh_length=45
--set hand width
mh_width=3
--set hand line cap
mh_cap=CAIRO_LINE_CAP_ROUND
--set minute hand color
mhr,mhg,mhb,mha=0,1,0,1--fully opaque white
--hour hand setup
--set length of hour hand
hh_length=30
--set hand width
hh_width=5
--set hand line cap
hh_cap=CAIRO_LINE_CAP_ROUND
--set hour hand color
hhr,hhg,hhb,hha=0,1,0,1--fully opaque white

--DRAWING CODE
--draw border
cairo_set_source_rgba (cr,cbr,cbg,cbb,cba)
cairo_set_line_width (cr,clock_border_width)
cairo_arc (cr,clock_centerx,clock_centery,clock_radius,0,2*math.pi)
cairo_stroke (cr)
--draw marks
--stuff that can be moved outside of the loop, needs only be set once
--calculate end and start radius for marks
m_end_rad=clock_radius-b_to_m
m_start_rad=m_end_rad-m_length
--set line cap type
cairo_set_line_cap  (cr, m_cap)
--set line width
cairo_set_line_width (cr,m_width)
--set color and alpha for marks
cairo_set_source_rgba (cr,mr,mg,mb,ma)
--start for loop
for i=1,12 do
--drawing code uisng the value of i to calculate degrees
--calculate start point for 12 oclock mark
radius=m_start_rad
point=(math.pi/180)*((i-1)*30)
x=0+radius*(math.sin(point))
y=0-radius*(math.cos(point))
--set start point for line
cairo_move_to (cr,clock_centerx+x,clock_centery+y)
--calculate end point for 12 oclock mark
radius=m_end_rad
point=(math.pi/180)*((i-1)*30)
x=0+radius*(math.sin(point))
y=0-radius*(math.cos(point))
--set path for line
cairo_line_to (cr,clock_centerx+x,clock_centery+y)
--draw the line
cairo_stroke (cr)
end--of for loop

--time calculations###########################
hours=tonumber(os.date("%I"))--12 hour clock
--convert hours to seconds
h_to_s=hours*60*60
minutes=tonumber(os.date("%M"))
--convert minutes to seconds
m_to_s=minutes*60
--get current seconds
seconds=tonumber(os.date("%S"))

--draw hours hand ############################
--get hours minutes seconds as just seconds
hsecs=h_to_s+m_to_s+seconds
--calculate degrees for the hand each second
hsec_degs=hsecs*(360/(60*60*12))--im using an equation instead of typing the calculation straight in because the result of 360/43200 gave us decimal places
--set radius we will use to calculate hand points
radius=hh_length
--set our starting line coordinates, the center of the circle
cairo_move_to (cr,clock_centerx,clock_centery)
--calculate coordinates for end of minutes hand
point=(math.pi/180)*hsec_degs
x=0+radius*(math.sin(point))
y=0-radius*(math.cos(point))
--describe the line we will draw
cairo_line_to (cr,clock_centerx+x,clock_centery+y)
--set up line attributes and draw line
cairo_set_line_width (cr,hh_width)
cairo_set_source_rgba (cr,hhr,hhg,hhb,hha)
cairo_set_line_cap  (cr, hh_cap)
cairo_stroke (cr)

--draw minutes hand
--get minutes and seconds just as seconds
msecs=m_to_s+seconds
--calculate degrees for the hand each second
msec_degs=msecs*0.1
--set radius we will use to calculate hand points
radius=mh_length
--set our starting line coordinates, the center of the circle
cairo_move_to (cr,clock_centerx,clock_centery)
--calculate coordinates for end of minutes hand
point=(math.pi/180)*msec_degs
x=0+radius*(math.sin(point))
y=0-radius*(math.cos(point))
--describe the line we will draw
cairo_line_to (cr,clock_centerx+x,clock_centery+y)
--set up line attributes and draw line
cairo_set_line_width (cr,mh_width)
cairo_set_source_rgba (cr,mhr,mhg,mhb,mha)
cairo_set_line_cap  (cr, mh_cap)
cairo_stroke (cr)

--draw seconds hand #############################
--calculate degrees for the hand each second
sec_degs=seconds*6
--set radius we will use to calculate hand points
radius=sh_length
--set our starting line coordinates, the center of the circle
cairo_move_to (cr,clock_centerx,clock_centery)
--calculate coordinates for end of seconds hand
point=(math.pi/180)*sec_degs
x=0+radius*(math.sin(point))
y=0-radius*(math.cos(point))
--describe the line we will draw
cairo_line_to (cr,clock_centerx+x,clock_centery+y)
--set up line attributes
cairo_set_line_width (cr,sh_width)
cairo_set_source_rgba (cr,shr,shg,shb,sha)
cairo_set_line_cap  (cr, sh_cap)
cairo_stroke (cr)

--#########################################################################################################
cairo_destroy(cr)
cairo_surface_destroy(cs)
cr=nil
end-- end main function
This is Alison Pitt's Air Clock,
scrot.png

Code: Select all

--[[
Air Clock by Alison Pitt (2009)
This clock is designed to look like KDE 4.3's "Air" clock, but from inside Conky.
You can adjust the clock's radius and placement, as well as the size and offset of the drop shadow. You can also choose whether to display the seconds hand. This clock updates every time Conky does, so if you want to show seconds, it is recommended that you set update_interval to no more than 0.5s. If you turn off seconds, you can set the update_interval to as long as 30s.  The settings are in the "Settings" section, starting at Line 21.
Call this script in Conky using the following before TEXT (assuming you save this script to ~/scripts/clock.lua):
	lua_load ~/scripts/clock.lua
	lua_draw_hook_pre draw_clock
]]

require 'cairo'
function conky_draw_clock()
	if conky_window==nil then return end
	local w=conky_window.width
	local h=conky_window.height
	local cs=cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, w, h)
	cr=cairo_create(cs)
			
	-- Settings
	
		-- What radius should the clock face (not including border) be, in pixels?
		
		local clock_r=46
	
		-- x and y coordinates, relative to the top left corner of Conky, in pixels
		
		local xc=167
		local yc=175
	
		-- Extent of the shadow, in pixels
		
		shadow_width=3
		
		-- x and y offsets of the drop shadow, relative to the centre of the clock face, in pixels. Can be positive (downward) or negative (upward)
		
		shadow_xoffset=0
		shadow_yoffset=0
		
		-- Do you want to show the second hand? Use this if you use a Conky update_interval > 1s. Can be true or false.
		
		show_seconds=true

	-- Grab time
	
	local hours=os.date("%I")
	local mins=os.date("%M")
	local secs=os.date("%S")
	
	secs_arc=(2*math.pi/60)*secs
	mins_arc=(2*math.pi/60)*mins
	hours_arc=(2*math.pi/12)*hours+mins_arc/12
	
	-- Drop shadow
	
	local ds_pat=cairo_pattern_create_radial(xc+shadow_xoffset,yc+shadow_yoffset,clock_r*1.25,xc+shadow_xoffset,yc+shadow_yoffset,clock_r*1.25+shadow_width)
	cairo_pattern_add_color_stop_rgba(ds_pat,0,0,0,0,0.2)
	cairo_pattern_add_color_stop_rgba(ds_pat,1,0,0,0,0)
	
	cairo_move_to(cr,0,0)
	cairo_line_to(cr,w,0)
	cairo_line_to(cr,w,h)
	cairo_line_to(cr,0,h)
	cairo_new_sub_path(cr)
	cairo_arc(cr,xc,yc,clock_r*1.25,0,2*math.pi)
	cairo_set_source(cr,ds_pat)
	cairo_set_fill_rule(cr,CAIRO_FILL_RULE_EVEN_ODD)
	cairo_fill(cr)
	
	-- Glassy border
	
	cairo_arc(cr,xc,yc,clock_r*1.25,0,2*math.pi)
	cairo_set_source_rgba(cr,0.5,0.5,0.5,0.2)
	cairo_set_line_width(cr,1)
	cairo_stroke(cr)
	
	local border_pat=cairo_pattern_create_linear(xc,yc-clock_r*1.25,xc,yc+clock_r*1.25)
	
	cairo_pattern_add_color_stop_rgba(border_pat,0,1,1,1,0.7)
	cairo_pattern_add_color_stop_rgba(border_pat,0.3,1,1,1,0)
	cairo_pattern_add_color_stop_rgba(border_pat,0.5,1,1,1,0)
	cairo_pattern_add_color_stop_rgba(border_pat,0.7,1,1,1,0)
	cairo_pattern_add_color_stop_rgba(border_pat,1,1,1,1,0.7)
	cairo_set_source(cr,border_pat)
	cairo_arc(cr,xc,yc,clock_r*1.125,0,2*math.pi)
	cairo_close_path(cr)
	cairo_set_line_width(cr,clock_r*0.25)
	cairo_stroke(cr)
	
	-- Set clock face
	
	cairo_arc(cr,xc,yc,clock_r,0,2*math.pi)
	cairo_close_path(cr)
	
	local face_pat=cairo_pattern_create_radial(xc,yc-clock_r*0.75,0,xc,yc,clock_r)
	
	cairo_pattern_add_color_stop_rgba(face_pat,0,1,1,1,0.9)
	cairo_pattern_add_color_stop_rgba(face_pat,0.3,0,1,0,0.9)
	cairo_pattern_add_color_stop_rgba(face_pat,1,0.9,0.9,0.9,0.9)
	cairo_set_source(cr,face_pat)
	cairo_fill_preserve(cr)
	cairo_set_source_rgba(cr,0.5,0.5,0.5,0.2)
	cairo_set_line_width(cr, 1)
	cairo_stroke (cr)
	
	-- Draw hour hand
	
	xh=xc+0.7*clock_r*math.sin(hours_arc)
	yh=yc-0.7*clock_r*math.cos(hours_arc)
	cairo_move_to(cr,xc,yc)
	cairo_line_to(cr,xh,yh)
	
	cairo_set_line_cap(cr,CAIRO_LINE_CAP_ROUND)
	cairo_set_line_width(cr,5)
	cairo_set_source_rgba(cr,0,0,0,0.5)
	cairo_stroke(cr)
	
	-- Draw minute hand
	
	xm=xc+0.9*clock_r*math.sin(mins_arc)
	ym=yc-0.9*clock_r*math.cos(mins_arc)
	cairo_move_to(cr,xc,yc)
	cairo_line_to(cr,xm,ym)
	
	cairo_set_line_width(cr,3)
	cairo_stroke(cr)
	
	-- Draw seconds hand
	
	if show_seconds then
		xs=xc+0.9*clock_r*math.sin(secs_arc)
		ys=yc-0.9*clock_r*math.cos(secs_arc)
		cairo_move_to(cr,xc,yc)
		cairo_line_to(cr,xs,ys)
	
		cairo_set_line_width(cr,1)
		cairo_stroke(cr)
                cairo_destroy(cr)
	        cairo_surface_destroy(c)
	end
        
end
Mr. Peachy's clock has a dial with those cool markings and uses 0.01% less memory, Alison Pitt's Air Clock has a very attractive glassy border. What i am trying to do is get a marked dial in the latter or a glassy border in the former..i am hoping this silly exercise will help me get some sort of a teensy bit of handle on how the code works. Join me if you will, please! :)
Edit: Yaba-daba-doo! Mr. Peachy's it is, with Alison Pitt's Glassy border.
scrot.png
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Linux User #481272 Reg: 15th Sept., 2008
User avatar
mzsade
Level 5
Level 5
Posts: 776
Joined: Sun Jul 19, 2009 4:36 am

Re: [Solved]Mix-n-Match clock.lua codes

Post by mzsade »

Clock with numbered dial (boorish ugly hack)

Code: Select all

cairo_select_font_face(cr, "DejaVu Sans Mono", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD)
cairo_set_font_size(cr, 12)
for i=1,12 do
--drawing code uisng the value of i to calculate degrees
--calculate start point for 12 oclock mark
radius=37
point=(math.pi/180)*((i)*30)
x=0+radius*(math.sin(point))
y=0-radius*(math.cos(point))
--set start point
cairo_move_to (cr,clock_centerx+x-3.5,clock_centery+y+3.5)
cairo_show_text (cr,i)
end--of for loop

Clock with Roman Numerals, this was extremely tedious, for me. :lol:
Linux User #481272 Reg: 15th Sept., 2008
Locked

Return to “Compiz, Conky, Docks & Widgets”