Just copy the file "palette.ms" here to "/usr/ lib" and you can import it as a library in your scripts.
// A simple palette set
// You can add many palettes of different sizes and then select any of them by name
// e.g.:
// palette.addPalette "IceCreamGB", ["#7c3f58", "#eb6b6f", "#f9a875", "#fff6d3"]
// palette.addPalette "TempleRuins", ["#ba5258" ,"#9e4e58" ,"#744758" ,"#563d4e" ,"#493443" ,"#261c22"]
// palette.setPalette "IceCreamGB"
_p = {}
_p.currentPaletteName = ""
_p.currentPaletteColors = []
_p.palettesMap = {}
// palettes methods //
addPalette = function (paletteName, colors)
if paletteName == "" or paletteName == null then
print "Error in 'addPalette': palette name is empty"
return false
end if
if not (colors isa list) or colors == null or colors.len <= 0 then
print "Error in 'addPalette': colors not defined"
return false
end if
_p.palettesMap[paletteName] = colors
end function
setPalette = function (paletteName)
if not _p.palettesMap.hasIndex(paletteName) then
print "Error in 'setPalette': palette " + paletteName + " not defined, use 'addPalette' before 'setPalette'"
return false
end if
_p.currentPaletteName = paletteName
_p.currentPaletteColors = _p.palettesMap[paletteName]
end function
hasPalette = function (paletteName)
return _p.palettesMap.hasIndex(paletteName)
end function
getPaletteName = function ()
return _p.currentPaletteName
end function
logPalettes = function ()
textCol = text.color
print "Palettes:"
for name in _p.palettesMap.indexes
colors = _p.palettesMap[name]
text.delimiter = ""
print " " + name + ", len: " + colors.len + " ["
text.inverse = true
for col in colors
text.color = col
print " "
end for
text.inverse = false
text.color = textCol
print "]"
text.delimiter = char(13)
print " "
end for
end function
// colors methods //
getColor = function (colorIx)
signIX = sign(colorIx)
newColorIx = abs(colorIx) % _p.currentPaletteColors.len * signIX
return _p.currentPaletteColors[newColorIx]
end function
getColorIX = function (color)
for i in range(0, _p.currentPaletteColors.len-1)
if _p.currentPaletteColors[i] == color then return i
end for
return -1
end function
hasColor = function (color)
for i in range(0, _p.currentPaletteColors.len-1)
if _p.currentPaletteColors[i] == color then return true
end for
return false
end function
getColorsLen = function ()
return _p.currentPaletteColors.len
end function
//----------------------------------------------------------------------
// Unit tests (run when you load & run this script directly).
runUnitTests = function()
print "Unit testing: palette"
errorCount = 0
assertEqual = function(actual, expected)
if actual != expected then
print "Unit test failure: expected " + expected + ", got " + actual
outer.errorCount = errorCount + 1
end if
end function
assertEqualEither = function(actual, expected, expected2)
if actual != expected and actual != expected2 then
print "Unit test failure: expected " + expected + ", got " + actual
outer.errorCount = errorCount + 1
end if
end function
paletteName = "IceCreamGB"
col_0 = "#7c3f58"
col_1 = "#eb6b6f"
col_2 = "#f9a875"
col_3 = "#fff6d3"
addPalette paletteName, [col_0, col_1, col_2, col_3]
setPalette paletteName
assertEqual setPalette("abcde"), false
assertEqual addPalette(""), false
assertEqual addPalette("abcde", []), false
assertEqual getColor(0), col_0
assertEqual getColor(getColorsLen + 1), col_1
assertEqual getColor(-1), col_3
assertEqual getColor(-getColorsLen - 1), col_3
assertEqual getColorIX(col_0), 0
assertEqual getColorIX(col_2), 2
assertEqual getColorIX("#ffffff"), -1
assertEqual hasColor(col_3), true
assertEqual hasColor("#ffffff"), false
assertEqual getColorsLen, 4
if errorCount == 0 then
print "All tests passed. Hurrah!"
else
print errorCount + " error" + "s" * (errorCount!=1) + " found."
end if
end function
if locals == globals then runUnitTests
An example of using palettes:
import "palette"
// init palettes
iceCreamGB = "IceCreamGB"
palette.addPalette iceCreamGB, ["#7c3f58", "#eb6b6f", "#f9a875", "#fff6d3"]
templeRuins = "TempleRuins"
palette.addPalette templeRuins, ["#ba5258" ,"#9e4e58" ,"#744758" ,"#563d4e" ,"#493443" ,"#261c22"]
pico8 = "PICO-8"
palette.addPalette pico8, ["#000000" ,"#1D2B53" ,"#7E2553" ,"#008751" ,"#AB5236" ,"#5F574F" ,"#C2C3C7" ,"#FFF1E8" ,"#FF004D" ,"#FFA300" ,"#FFEC27" ,"#00E436" ,"#29ADFF" ,"#83769C" ,"#FF77A8" ,"#FFCCAA"]
myPalettesNames = [iceCreamGB, templeRuins, pico8]
initPalette = function (ix)
clear
text.inverse = true
print "Set palette: " + myPalettesNames[ix]
print "Press any key to select next palette"
text.inverse = false
palette.setPalette myPalettesNames[ix]
gfx.clear getRndColor
end function
getRndColor = function ()
rndColIX = palette.getColorsLen * rnd
return palette.getColor (rndColIX)
end function
boxSize = 40
palIX = 0
// init first palette
initPalette(palIX)
// main loop
while not key.pressed("escape") and not key.pressed("q")
// change palette by pressing any key
if key.available then
palIX = palIX + 1
if palIX >= myPalettesNames.len then palIX = 0
initPalette palIX
key.clear
end if
// to draw box by random color from palette
gfx.color = getRndColor
x = gfx.width * rnd
y = gfx.height * rnd
w = boxSize + boxSize * rnd
h = boxSize + boxSize * rnd
gfx.fillRect x, y, w, h
yield
end while