Module:Sandbox/Drewmutt/SwatchGrid

Revision as of 06:18, 16 December 2017 by imported>Drewmutt
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:Sandbox/Drewmutt/SwatchGrid/doc

local p = {}


function rgbToHsl(r, g, b)
    r, g, b = r / 255, g / 255, b / 255

    local max, min = math.max(r, g, b), math.min(r, g, b)
    local hue, sat, level

    level = (max + min) / 2

    if max == min then
        hue, sat = 0, 0 -- achromatic
    else
        local d = max - min
        if level > 0.5 then sat = d / (2 - max - min) else sat = d / (max + min) end
        if max == r then
            hue = (g - b) / d
            if g < b then hue = hue + 6 end
        elseif max == g then hue = (b - r) / d + 2
        elseif max == b then hue = (r - g) / d + 4
        end
        hue = hue / 6
    end

    return hue, sat, level
end


local tileSizeW
local tileSizeH
local tileSpacingW
local tileSpacingH
	
function getSwatchTileString(r, g, b)
    local divstart = "<div style='margin-left:"..tileSpacingW..";margin-right:"..tileSpacingW..";display:inline-block;width:"..tileSizeW..";height:"..tileSizeH..";background-color:HSL("
    local divend = "%)'>&nbsp;</div>"
    local a2, b2, c2 = rgbToHsl(r, g, b)
    return divstart .. (a2 * 360) .. "," .. (b2 * 100) .. "%," .. (c2 * 100) .. divend
end

function p.swatchgrid(frame)
    local a = frame.args
    local colors = {}
    colors[1] = { a[5], a[6], a[7] }
    colors[2] = { a[8], a[9], a[10] }
    colors[3] = { a[11], a[12], a[13] }
    colors[4] = { a[14], a[15], a[16] }
    colors[5] = { a[17], a[18], a[19] }

	 tileSizeW = a[1]
	 tileSizeH = a[2]
	tileSpacingW = a[3]
	tileSpacingH = a[4]
    local tiles = 5
    local hdivide = "<div style='height:"..tileSpacingH.."'></div>"
    local result = ""

    for i = 1, tiles, 1
    do
        local color = colors[i] local h = color[1] local s = color[2] local v = color[3]
        s = s / 2
        result = result .. getSwatchTileString(h, s, v)
    end
    result = result .. hdivide

    for i = 1, tiles, 1
    do
        local color = colors[i] local h = color[1] local s = color[2] local v = color[3]
        result = result .. getSwatchTileString(h, s, v)
    end
    result = result .. hdivide

    for i = 1, tiles, 1
    do
        local color = colors[i] local h = color[1] local s = color[2] local v = color[3]
        v = v / 2
        result = result .. getSwatchTileString(h, s, v)
    end
    result = result .. hdivide

    for i = 1, tiles, 1
    do
        local color = colors[i] local h = color[1] local s = color[2] local v = color[3]
        h = (h + 20) % 360
        result = result .. getSwatchTileString(h, s, v)
    end
    result = result .. hdivide

    for i = 1, tiles, 1
    do
        local color = colors[i] local h = color[1] local s = color[2] local v = color[3]
        h = (h + 40) % 360
        result = result .. getSwatchTileString(h, s, v)
    end
    result = result .. hdivide

    return result
end

return p