diff --git a/src/cairo_helpers.lua b/src/cairo_helpers.lua index 04d2afd..2096752 100644 --- a/src/cairo_helpers.lua +++ b/src/cairo_helpers.lua @@ -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 @@ -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 diff --git a/src/widget.lua b/src/widget.lua index c8dd822..abcc9cf 100644 --- a/src/widget.lua +++ b/src/widget.lua @@ -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])