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

Fixes #8 #9

Merged
merged 3 commits into from
Jul 10, 2024
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
8 changes: 8 additions & 0 deletions lua/convert/converters.lua
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ converters['in'] = {

local function hex_to_rgb(val)
local hex = val:gsub("#", "")

if #hex == 3 then
local r = tonumber(hex:sub(1, 1) .. hex:sub(1, 1), 16)
local g = tonumber(hex:sub(2, 2) .. hex:sub(2, 2), 16)
local b = tonumber(hex:sub(3, 3) .. hex:sub(3, 3), 16)
return string.format("rgb(%d, %d, %d)", r, g, b)
end

local r = tonumber(hex:sub(1, 2), 16)
local g = tonumber(hex:sub(3, 4), 16)
local b = tonumber(hex:sub(5, 6), 16)
Expand Down
2 changes: 1 addition & 1 deletion lua/convert/ui/open_popup.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ M.open_win = function(found_unit)
border = {
style = "rounded",
text = {
top = "[Convert " .. found_unit.val .. found_unit.unit .. " To]",
top = "[Convert " .. found_unit.val .. " To]",
top_align = "center"
},
},
Expand Down
16 changes: 8 additions & 8 deletions tests/convert/converter_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ local units = {

local size_values = {
px = 16,
rem = 1,
rem = 1.0,
mm = 4.23,
['in'] = 0.17,
cm = 0.42,
Expand All @@ -26,20 +26,20 @@ describe("Unit Conversions", function()
for i = 1, #units, 1 do
local round = 2

if to_unit == 'px' then
round = 0
end

local from_unit = unit
local to_unit = units[i]
local from_unit = unit

if from_unit == to_unit then
goto continue
end

if to_unit == 'px' or to_unit == 'rem' then
round = 0
end

it(string.format("Should convert %s, to %s", from_unit, to_unit), function()
local converted = utils.round(converters(from_unit, to_unit, value), round)
print(from_unit, to_unit, converted)
assert.are.equal(converted, utils.round(size_values[to_unit], round))
assert.are.equal(size_values[to_unit], converted)
end)
end
::continue::
Expand Down