Wednesday, August 8, 2012

Conky Lua Scripts (color.lua and queue.lua)

This is a queue implementation:

-- I more or less copied http://www.lua.org/pil/11.4.html
do
 Queue = {}
 function Queue.new(cap)
  return { first = 0, last = -1, capacity = cap }
 end
 function Queue.enqueue(queue, value)
  local last = queue.last + 1
  queue.last = last
  queue[last] = value
  if queue.last - queue.first + 1 > queue.capacity then Queue.dequeue(queue) end
 end
 function Queue.dequeue(queue)
  local first = queue.first
  if first > queue.last then error("queue is empty") end
  local value = queue[first]
  queue[first] = nil        -- to allow garbage collection
  queue.first = first + 1
  return value
 end
 function Queue.length()
  return last - first + 1;
 end
end

This will convert percents to colors (0% is blue or green, 100% is red) and it'll do HSV to RGB conversion (most important thing out of the HSV stuff is that varying H from 0 to 360 lets you roll around the color wheel).

do
 function percent2rb(percent)
  local p = tonumber(percent)
  if not p then p = 0 end
  if p > 100 then p = 100 end
  local red = math.floor(0xff * p / 100);
  local blue = 0xff - red; --math.floor(0xff * (100 - p) / 100);
  return red * 0x10000 + blue;
 end
 function percent2rg(percent)
  local p = tonumber(percent)
  if not p then p = 0 end
  if p > 100 then p = 100 end
  local red = math.floor(0xff * p / 100);
  local green = 0xff - red; --math.floor(0xff * (100 - p) / 100);
  return red * 0x10000 + green * 0x100;
 end
 function hsv2rgb(h, s, v)
  h = h / 360;
  local r, g, b
  local i = math.floor(h * 6)
  local f = h * 6 - i
  local p = v * (1 - s)
  local q = v * (1 - f * s)
  local t = v * (1 - (1 - f) * s)

  local switch = i % 6
  if  switch == 0 then r = v g = t b = p
  elseif  switch == 1 then r = q g = v b = p
  elseif  switch == 2 then r = p g = v b = t
  elseif  switch == 3 then r = p g = q b = v
  elseif  switch == 4 then r = t g = p b = v
  elseif  switch == 5 then r = v g = p b = q
  end
  
  r = math.floor(r * 0xff) g = math.floor(g * 0xff) b = math.floor(b * 0xff)
  return r * 0x10000 + g * 0x100 + b; 
 end
end

No comments:

Post a Comment