Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

write_left,right rounds to pixel. temperature_color robus to nil temp #3

Merged
merged 3 commits into from
Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/cairo_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,24 @@ local function text_extents(cr, text)
return extents
end

--- Round coordinates to even pixel values
-- @see https://www.cairographics.org/FAQ/#sharp_lines
-- @see https://scriptinghelpers.org/questions/4850/how-do-i-round-numbers-in-lua-answered
-- @tparam cairo_t cr
-- @number x horizontal pixel coord value
-- @number y vertical pixel coord value
local function round_coords(cr, x, y)
local u, v = cairo_user_to_device(cr, x, y)
return cairo_device_to_user(cr, math.floor(u+0.5), math.floor(v+0.5))
end

--- Write text left-aligned (to the right of given x).
-- @tparam cairo_t cr
-- @number x start of the written text
-- @number y coordinate of the baseline on top of which the text will be written
-- @string text
function ch.write_left(cr, x, y, text)
x, y = round_coords(cr, x, y)
cairo_move_to(cr, x, y)
cairo_show_text(cr, text)
end
Expand All @@ -173,6 +185,7 @@ end
-- @number y coordinate of the baseline on top of which the text will be written
-- @string text
function ch.write_right(cr, x, y, text)
x, y = round_coords(cr, x, y)
cairo_move_to(cr, x - text_extents(cr, text).width, y)
cairo_show_text(cr, text)
end
Expand Down
14 changes: 10 additions & 4 deletions src/widget.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,16 @@ local temperature_colors = {
-- @number low threshold for lowest temperature / coolest color
-- @number high threshold for highest temperature / hottest color
function w.temperature_color(temperature, low, high)
local idx = (temperature - low) / (high - low) * (#temperature_colors - 1) + 1
local weight = idx - floor(idx)
local cool = temperature_colors[clamp(1, #temperature_colors, floor(idx))]
local hot = temperature_colors[clamp(1, #temperature_colors, ceil(idx))]
-- defaults in case temperature is nil
local cool = temperature_colors[1]
local hot = temperature_colors[1]
local weight = 0
if type(temperature) == "number" and temperature > -math.huge and temperature < math.huge then
local idx = (temperature - low) / (high - low) * (#temperature_colors - 1) + 1
weight = idx - floor(idx)
cool = temperature_colors[clamp(1, #temperature_colors, floor(idx))]
hot = temperature_colors[clamp(1, #temperature_colors, ceil(idx))]
end
return cool[1] + weight * (hot[1] - cool[1]),
cool[2] + weight * (hot[2] - cool[2]),
cool[3] + weight * (hot[3] - cool[3])
Expand Down