FANDOM


--[[-- 
    Color modification functions for Module:Colors
 
    Notes:
    - First argument always contains the base Color object
    - Every mod function should return a new Color object or nil (no change)
 
    - List of strings in the table contains data types:
      - color:
        Takes any supported color format and returns the Color object
 
      - number:
        Returns a number
 
      - rgb:
        Returns a number where 255 input is equal to 1 on the output
 
      - degrees:
        Returns: number between -360 and 360
 
        You can also use s_rgb, s_number and s_degrees
        for functions to additionaly return a flag if the input was signed or not
        (to differentiate between "10%" and "+10%")
--]]--
function isColor(val)
    if type(val) == 'table' and type(val.toHEX) == 'function' then return true end
    return false
end
local mods = {}
 
mods['mix'] = {
    args = {'color', 'number'},
    func = function(c1, c2, w2)
        local w2 = math.max(0, math.min(1, w2 or 0.5))
        if w2 == 0 or c2 == nil then return nil end
        local w1 = 1 - w2
        return mods.Color.newRGB(
            c1:r() * w1 + c2:r() * w2,
            c1:g() * w1 + c2:g() * w2,
            c1:b() * w1 + c2:b() * w2,
            c1:a() * w1 + c2:a() * w2
        )
    end
}
mods['gmix'] = {
    args = {'color', 'number'},
    func = function(c1, c2, w2)
        local w2 = math.max(0, math.min(1, w2 or 0.5))
        if w2 == 0 or c2 == nil then return nil end
        local w1 = 1 - w2
        return mods.Color.newRGB(
            math.sqrt(c1:r()^2 * w1 + c2:r()^2 * w2),
            math.sqrt(c1:g()^2 * w1 + c2:g()^2 * w2),
            math.sqrt(c1:b()^2 * w1 + c2:b()^2 * w2),
            c1:a() * w1 + c2:a() * w2
        )
    end
}
mods['multiply'] = {
    args = {'color'},
    func = function(c1, c2)
        local w2 = math.max(0, math.min(1, c2:a() or 1))
        if w2 == 0 or c2 == nil then return nil end
        local w1 = 1 - w2
        return mods.Color.newRGB(
            c1:r() * w1 + c1:r() * c2:r() * w2,
            c1:g() * w1 + c1:g() * c2:g() * w2,
            c1:b() * w1 + c1:b() * c2:b() * w2
        )
    end
}
mods['screen'] = {
    args = {'color'},
    func = function(c1, c2)
        local w2 = math.max(0, math.min(1, c2:a() or 1))
        if w2 == 0 or c2 == nil then return nil end
        local w1 = 1 - w2
        return mods.Color.newRGB(
            c1:r() * w1 + (1 - ((1 - c1:r()) * (1 - c2:r()))) * w2,
            c1:g() * w1 + (1 - ((1 - c1:g()) * (1 - c2:g()))) * w2,
            c1:b() * w1 + (1 - ((1 - c1:b()) * (1 - c2:b()))) * w2
        )
    end
}
mods['color'] = {
    args = {'color'},
    func = function(c1, c2)
        local w2 = math.max(0, math.min(1, c2:a() or 1))
        if w2 == 0 or c2 == nil then return nil end
        local w1 = 1 - w2
        return c1:copy():setHSL(
            c1:h() * w1 + c2:h() * w2,
            c1:s() * w1 + c2:s() * w2
        )
    end
}
mods['shade'] = {
    args = {'number'},
    func = function(c, w)
        local w = 1 - math.max(0, math.min(1, w or 0.1))
        if w == 0 then return nil end
        return c:copy():setRGB(
            c:r() * w,
            c:g() * w,
            c:b() * w
        )
    end
}
mods['tint'] = {
    args = {'number'},
    func = function(c, w1)
        local w1 = math.max(0, math.min(1, w1 or 0.1))
        if w1 == 0 then return nil end
        local w2 = 1 - w1
        return c:copy():setRGB(
            w1 + c:r() * w2,
            w1 + c:g() * w2,
            w1 + c:b() * w2
        )
    end
}
 
 
 
mods['red'] = {
    args = {'color or s_rgb'},
    func = function(c, value)
        local num, flag
        if isColor(value) then num = value:r()
        else num, flag = unpack(value) end
        if flag then
            return c:copy():setR(c:r() + num)
        else
            return c:copy():setR(num)
        end
    end
}
mods['green'] = {
    args = {'color or s_rgb'},
    func = function(c, value)
        local num, flag
        if isColor(value) then num = value:g()
        else num, flag = unpack(value) end
        if flag then
            return c:copy():setG(c:g() + num)
        else
            return c:copy():setG(num)
        end
    end
}
mods['blue'] = {
    args = {'color or s_rgb'},
    func = function(c, value)
        local num, flag
        if isColor(value) then num = value:b()
        else num, flag = unpack(value) end
        if flag then
            return c:copy():setB(c:b() + num)
        else
            return c:copy():setB(num)
        end
    end
}
mods['rbg'] = {
    func = function(c)
        return mods.Color.newRGB(
            c:r(),
            c:b(),
            c:g()
        )
    end
}
mods['grb'] = {
    func = function(c)
        return mods.Color.newRGB(
            c:g(),
            c:r(),
            c:b()
        )
    end
}
mods['bgr'] = {
    func = function(c)
        return mods.Color.newRGB(
            c:b(),
            c:g(),
            c:r()
        )
    end
}
mods['gbr'] = {
    func = function(c)
        return mods.Color.newRGB(
            c:g(),
            c:b(),
            c:r()
        )
    end
}
mods['bgr'] = {
    func = function(c)
        return mods.Color.newRGB(
            c:b(),
            c:g(),
            c:r()
        )
    end
}
mods['brg'] = {
    func = function(c)
        return mods.Color.newRGB(
            c:b(),
            c:r(),
            c:g()
        )
    end
}
 
 
 
 
mods['hue'] = {
    args = {'color or s_degrees'},
    func = function(c, value)
        local num, flag
        if isColor(value) then num = value:h()
        else num, flag = unpack(value) end
        if flag then
            return c:copy():setH(c:h() + num)
        else
            return c:copy():setH(num)
        end
    end
}
mods['saturation'] = {
    args = {'color or s_number'},
    func = function(c, value)
        local num, flag
        if isColor(value) then num = value:s()
        else num, flag = unpack(value) end
        if flag then
            return c:copy():setS(c:s() + num)
        else
            return c:copy():setS(num)
        end
    end
}
mods['lightness'] = {
    args = {'color or s_number'},
    func = function(c, value)
        local num, flag
        if isColor(value) then num = value:l()
        else num, flag = unpack(value) end
        if flag then
            return c:copy():setL(c:l() + num)
        else
            return c:copy():setL(num)
        end
    end
}
mods['saturate'] = {
    args = {'number'},
    func = function(c, num)
        return c:copy():setS(c:s() + (num or .1))
    end
}
mods['desaturate'] = {
    args = {'number'},
    func = function(c, num)
        return c:copy():setS(c:s() - (num or .1))
    end
}
mods['lighten'] = {
    args = {'number'},
    func = function(c, num)
        return c:copy():setL(c:l() + (num or .1))
    end
}
mods['darken'] = {
    args = {'number'},
    func = function(c, num)
        return c:copy():setL(c:l() - (num or .1))
    end
}
 
 
 
 
mods['alpha'] = {
    args = {'color or s_number'},
    func = function(c, value)
        local num, flag
        if isColor(value) then num = value:a()
        else num, flag = unpack(value) end
        if flag then
            return c:copy():setA(c:a() + num)
        else
            return c:copy():setA(num)
        end
    end
}
mods['opacify'] = {
    args = {'number'},
    func = function(c, num)
        return c:copy():setA(c:a() + (num or .1))
    end
}
mods['transparentize'] = {
    args = {'number'},
    func = function(c, num)
        return c:copy():setA(c:a() - (num or .1))
    end
}
mods['opacity'] = mods['alpha']
mods['fade-in'] = mods['opacify']
mods['fade-out'] = mods['transparentize']
 
 
 
 
mods['grayscale'] = {
    func = function(c)
        return c:copy():setS(0)
    end
}
mods['invert'] = {
    func = function(c)
        return c:copy():setRGB(
            1 - c:r(),
            1 - c:g(),
            1 - c:b()
        )
    end
}
mods['complement'] = {
    func = function(c)
        return c:copy():setH(c:h() + 180)
    end
}
mods['greyscale'] = mods['grayscale']
return mods

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.