NES Color Palette for Pixel Art

--[[ user: togagames for LuaSnippets.com and (user@) CoreGames.com

Some hex values from Pixel Editors will give you a value with "ff" at the beginning of the string
Put the 2 "f"s at the end instead (here).


Working Example of how to use:

-- Mimick the Palette onto the group of blocks 14 x 4 within a folder named: ClientContextCubes (grab MUID)
local _bfID = "582A82E06CEDF7D2:ClientContextCubes"
local _blocksFolder = World.FindObjectById(_bfID)
local _nextHex = 1
for _,child in ipairs(_blocksFolder:GetChildren()) do
    child:SetColor(Color.New(_color.hex(_color[_nextHex][5])))
    --child:SetColor(Color.New(_color.hex("007800ff")))
    _nextHex = _nextHex + 1
end
--]]

local _palette = { -- Core RGBA --> NES color palette (14x4 = 56) /w Hex sRGB equivalent values

    {"7c7c7cff"},
    {"0000fcff"},--2
    {"0000bcff"},
    {"4428bcff"},--4
    {"940084ff"},
    {"a80020ff"},--6
    {"a81000ff"},
    {"881400ff"},--8
    {"503000ff"},
    {"007800ff"},--10
    {"006800ff"},
    {"005800ff"},
    {"004058ff"},
    {"000000ff"},--14
    
    
    {"bcbcbcff"},--15
    {"4428bcff"},
    {"0058f8ff"},--17
    {"6844fcff"},
    {"d800ccff"},
    {"e40058ff"},--20
    {"f83800ff"},
    {"e45c10ff"},
    {"ac7c00ff"},
    {"00b800ff"},--24
    {"00a800ff"},
    {"00a844ff"},
    {"008888ff"},
    {"080808ff"},--28
    
    {"fcfcfcff"},
    {"3cbcfcff"},--30
    {"6888fcff"},
    {"9878f8ff"},
    {"f878f8ff"},
    {"f85898ff"},--34
    {"f87858ff"},
    {"fca044ff"},
    {"f8b800ff"},
    {"b8f818ff"},--38
    {"58d854ff"},
    {"58f898ff"},
    {"00e8d8ff"},
    {"7c7c7cff"},--42
    
    {"ffffffff"},
    {"a4e4fcff"},
    {"b8b8f8ff"},--45
    {"d8b8f8ff"},
    {"f8b8f8ff"},
    {"f8a4c0ff"},--48
    {"f0d0b0ff"},
    {"fce0a8ff"},
    {"f8d878ff"},
    {"d8f878ff"},
    {"b8f8b8ff"},--53
    {"b8f8d8ff"},
    {"00fcfcff"},
    {"d8d8d8ff"},--56
    
    {0}-- 57 (empty)

}

local function hex (hex, alpha)
    local redColor,greenColor,blueColor=hex:match('(..)(..)(..)')
    redColor, greenColor, blueColor = tonumber(redColor, 16)/255, tonumber(greenColor, 16)/255, tonumber(blueColor, 16)/255
    redColor, greenColor, blueColor = math.floor(redColor*100)/255, math.floor(greenColor*100)/255, math.floor(blueColor*100)/255
    if alpha == nil then
        return redColor, greenColor, blueColor
    elseif alpha > 1 then
        alpha = alpha / 100
    end
    return redColor, greenColor, blueColor, alpha
end

local function rgb (r, g, b, alpha)
    local redColor,greenColor,blueColor=r/255, g/255, b/255
    redColor, greenColor, blueColor = math.floor(redColor*100)/100, math.floor(greenColor*100)/100, math.floor(blueColor*100)/100
    if alpha == nil then
        return redColor, greenColor, blueColor
    elseif alpha > 1 then
        alpha = alpha / 100
    end
    return redColor, greenColor, blueColor, alpha
end

_palette.hex = hex
_palette.rgb = rgb

return _palette

Latest Posts