Advertisement
Guest User

Colour Change Rings for Conky Lua by mrmrwat

a guest
Mar 25th, 2013
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.74 KB | None | 0 0
  1. --[[
  2.     Colour Change Rings by mrmrwat (24.03.2013)
  3.     based on Ring Meters v.1.2.1 by londonali1010 (20.10.2009)
  4.     and inspired by PolarClock by pixelbreaker
  5.        
  6.     New features include support for color-change rings
  7.     and millisecond precision for smooth ring movement
  8.    
  9.     NB. Requires lua socket for socket.gettime() function
  10. --]]
  11.  
  12. settings_table = {
  13.     {
  14.         name = 'time',
  15.         arg = '%I',
  16.         max = 12,
  17.         bg_colour = 0xffffff,
  18.         bg_alpha = 0.1,
  19.         fg_colour = 'rainbow_bgr',
  20.         fg_alpha = 0.6,
  21.         x = 146, y = 165,
  22.         radius = 75,
  23.         thickness = 12,
  24.         start_angle = 0,
  25.         end_angle = 360
  26.     },
  27.     -- Uncomment below and comment above for 24 hour clock instead of 12 hour clock
  28.     --[[{
  29.         name = 'time',
  30.         arg = '%H',
  31.         max = 24,
  32.         bg_colour = 0xffffff,
  33.         bg_alpha = 0.1,
  34.         fg_colour = 'rainbow_bgr',
  35.         fg_alpha = 0.6,
  36.         x = 146, y = 165,
  37.         radius = 75,
  38.         thickness = 12,
  39.         start_angle = 0,
  40.         end_angle = 360
  41.     },]]
  42.     {
  43.         name = 'time',
  44.         arg = '%M',
  45.         max = 60,
  46.         bg_colour = 0xffffff,
  47.         bg_alpha = 0.1,
  48.         fg_colour = 'rainbow_bgr',
  49.         fg_alpha = 0.6,
  50.         x = 146, y = 165,
  51.         radius = 90,
  52.         thickness = 7,
  53.         start_angle = 0,
  54.         end_angle = 360
  55.     },
  56.     {
  57.         name = 'time',
  58.         arg = '%S',
  59.         max = 60,
  60.         bg_colour = 0xffffff,
  61.         bg_alpha = 0.1,
  62.         fg_colour = 'rainbow_bgr',
  63.         fg_alpha = 0.6,
  64.         x = 146, y = 165,
  65.         radius = 120,
  66.         thickness = 4,
  67.         start_angle = 0,
  68.         end_angle = 360
  69.     },
  70. }
  71.  
  72. require 'cairo'
  73. require 'socket'
  74.  
  75. function rgb_to_r_g_b(colour, alpha)
  76.     return ((colour / 0x10000) % 0x100) / 255.,
  77.         ((colour / 0x100) % 0x100) / 255.,
  78.         (colour % 0x100) / 255., alpha
  79. end
  80.  
  81. function draw_ring(cr, t, pt)
  82.     local w, h = conky_window.width, conky_window.height
  83.    
  84.     local xc, yc, ring_r, ring_w, sa, ea = pt['x'], pt['y'],
  85.         pt['radius'], pt['thickness'],
  86.         pt['start_angle'], pt['end_angle']
  87.     local bgc, bga, fgc, fga = pt['bg_colour'], pt['bg_alpha'],
  88.         pt['fg_colour'], pt['fg_alpha']
  89.  
  90.     local angle_0 = sa * (2 * math.pi / 360) - math.pi / 2
  91.     local angle_f = ea * (2 * math.pi / 360) - math.pi / 2
  92.     local t_arc = t * (angle_f - angle_0)
  93.  
  94.     -- Draw background ring
  95.    
  96.     cairo_arc(cr, xc, yc, ring_r, angle_0, angle_f)
  97.     cairo_set_source_rgba(cr, rgb_to_r_g_b(bgc, bga))
  98.     cairo_set_line_width(cr, ring_w)
  99.     cairo_stroke(cr)
  100.    
  101.     -- Draw indicator ring
  102.    
  103.     cairo_arc(cr, xc, yc, ring_r, angle_0, angle_0+t_arc)
  104.    
  105.     -- New if statement holds colour change logic
  106.    
  107.     local r = 0
  108.     local g = 0
  109.     local b = 0
  110.     if fgc == 'rainbow_rgb' then
  111.         if t < 1 / 6 then
  112.             r, g, b = 1, t * 6, 0
  113.         elseif t < 2 / 6 then
  114.             r, g, b = 1 - ((t * 6) - 1), 1, 0
  115.         elseif t < 3 / 6 then
  116.             r, g, b = 0, 1, (t * 6) - 2
  117.         elseif t < 4 / 6 then
  118.             r, g, b = 0, 1 - ((t * 6) - 3), 1
  119.         elseif t < 5 / 6 then
  120.             r, g, b = (t * 6) - 4, 0, 1
  121.         elseif t < 6 / 6 then
  122.             r, g, b = 1, 0, 1 - ((t * 6) - 5)
  123.         end
  124.         cairo_set_source_rgba(cr, r, g, b, fga)
  125.     elseif fgc == 'rainbow_bgr' then
  126.         if t < 1 / 6 then
  127.             r, g, b = 0, t * 6, 1
  128.         elseif t < 2 / 6 then
  129.             r, g, b = 0, 1, 1 - ((t * 6) - 1)
  130.         elseif t < 3 / 6 then
  131.             r, g, b = (t * 6) - 2, 1, 0
  132.         elseif t < 4 / 6 then
  133.             r, g, b = 1, 1 - ((t * 6) - 3), 0
  134.         elseif t < 5 / 6 then
  135.             r, g, b = 1, 0, (t * 6) - 4
  136.         elseif t < 6 / 6 then
  137.             r, g, b = 1 - ((t * 6) - 5), 0, 1
  138.         end
  139.         cairo_set_source_rgba(cr, r, g, b, fga)
  140.     elseif fgc == 'traffic' then
  141.         if t < 1 / 3 then
  142.             r, g, b = 0, 1, 0
  143.         elseif t < 2 / 3 then
  144.             r, g, b = (t * 3) - 1, 1, 0
  145.         elseif t < 3 / 3 then
  146.             r, g, b = 1, 1 - ((t * 3) - 2), 0
  147.         end
  148.         cairo_set_source_rgba(cr, r, g, b, fga)
  149.     elseif fgc == 'traffic_rev' then
  150.         if t < 1 / 3 then
  151.             r, g, b = 1, 0, 0
  152.         elseif t < 2 / 3 then
  153.             r, g, b = 1, (t * 3) - 1, 0
  154.         elseif t < 3 / 3 then
  155.             r, g, b = 1 - ((t * 3) - 2), 1, 0
  156.         end
  157.         cairo_set_source_rgba(cr, r, g, b, fga)
  158.     else
  159.         -- Original set_source code
  160.        
  161.         cairo_set_source_rgba(cr, rgb_to_r_g_b(fgc, fga))
  162.     end
  163.    
  164.     cairo_stroke(cr)
  165. end
  166.  
  167. function conky_ring_stats()
  168.     local function setup_rings(cr, pt)
  169.         local str = ''
  170.         local value = 0
  171.        
  172.         -- New if statement holds millisecond precision clock logic
  173.        
  174.         if pt['name'] == 'time' then
  175.             local epoch_time = os.time()
  176.             local mils = socket.gettime() - epoch_time
  177.             local secs = conky_parse('${time %S}')
  178.             secs = tonumber(secs) + mils
  179.             local mins = conky_parse('${time %M}')
  180.             mins = tonumber(mins)
  181.             local hrs  = conky_parse('${time %I}')
  182.             hrs  = tonumber(hrs)
  183.            
  184.             if pt['arg'] == '%I' then
  185.                 value = (hrs % 12) + (mins / 60) + (secs / 3600)
  186.             elseif pt['arg'] == '%H' then
  187.                 value = hrs + (mins / 60) + (secs / 3600)
  188.             elseif pt['arg'] == '%M' then
  189.                 value = mins + (secs / 60)
  190.             elseif pt['arg'] == '%S' then
  191.                 value = secs
  192.             else
  193.                 -- Original conky_parse code replicated here to prevent
  194.                 -- breakages in times which don't use %I, %H, %M, or %S
  195.                
  196.                 str = string.format('${%s %s}', pt['name'], pt['arg'])
  197.                 str = conky_parse(str)
  198.                 value = tonumber(str)
  199.             end
  200.         else
  201.             -- Original conky_parse code
  202.            
  203.             str = string.format('${%s %s}', pt['name'], pt['arg'])
  204.             str = conky_parse(str)
  205.             value = tonumber(str)
  206.         end
  207.        
  208.         pct = value / pt['max']
  209.        
  210.         draw_ring(cr, pct, pt)
  211.     end
  212.  
  213.     if conky_window == nil then return end
  214.    
  215.     local cs = cairo_xlib_surface_create(conky_window.display,
  216.         conky_window.drawable, conky_window.visual,
  217.         conky_window.width, conky_window.height)
  218.     local cr = cairo_create(cs)
  219.    
  220.     local updates = conky_parse('${updates}')
  221.     update_num = tonumber(updates)
  222.    
  223.     if update_num > 5 then
  224.         for i in pairs(settings_table) do
  225.             setup_rings(cr, settings_table[i])
  226.         end
  227.     end
  228. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement