Storing Color Data?

How would you do it? This is my code I’ve tried so far;

    clearLocalData()
    col2 = color(readLocalData("col2", 0, 199, 255, 255))
    col1 = color(readLocalData("col1", 0,125, 255, 0))
    colf = color(readLocalData("colf",0,0,0,255 ))
    bg = color(readLocalData("bg", 236, 236, 233, 255))

@Ignatz: nice! I would modify it a little to something like:

local rgba = color(255, 0, 0, 255)
saveLocalData("TEST", "return color"..tostring(rgba))
rgba = loadstring(readLocalData("TEST"))()

here is how I approached it a while ago:

local function string2rgb(str)
    local rgba = {}
    tostring(str):gsub("%d+", function(n)
        table.insert(rgba, n)
    end)
    return color(unpack(rgba))
end

parameter.color("Color", string2rgb(readLocalData("Color", color(41,151,176,255))))

you could make a very small image, write your colors pixel per pixel, and save it. Inverse process to read your colors.
Or save them as text: “0-128-132-255”, and decode by splitting the “-” and then tonumber()

store the colour as a line of code…

function setup()
    saveLocalData("color-a","a=color(100,100,50,255)")
    loadstring(readLocalData("color-a"))()
end

function draw()
    background(a)
end

You can convert the RGB to hex and store it like that, tho that wouldn’t keep the alpha stored… if alpha’s gonna be something else than 255 all the time, you could just add something after the hex so that it stores that as well

But for reading the hex, you’d need another piece of code

There are alot of possibilities tho… you could also just put every color after eachother and store that as a string/int, tho that would mean you have to make sure that there’s 001 stored in example of just ‘1’, since you need to split the string in reading the data again

You could also do what @Ignatz said, that’s an easy way to store the color

But as I said, there’s alot if possibilities

@ignatz nice one! I like it.

@Mr_Ninja, if @Ignatz his solution doesn’t work (idk when it wouldn’t work tho)

this is a small example of how you could convert a color to a hex string, and then convert it back to the color.

If you want to store the color, you ‘just’ have to convert the color to hex, store that hex
and when you want to retrieve the color, you put the hex-string in the convert function, and it gives you a color again

function setup()
    var = 255
    a = color(191, 111, 111, 190)
    print(color2hex(a))
    b = hex2color(color2hex(a))
    print(b)
end

function draw()
    
end

function num2hex(num)
    local hexstr = '0123456789abcdef'
    local s = ''
    while num > 0 do
        local mod = math.fmod(num, 16)
        s = string.sub(hexstr, mod+1, mod+1) .. s
        num = math.floor(num / 16)
    end
    if s == '' then s = '0' end
    return s
end

function color2hex(col)
    local hex = ''
    hex = hex .. num2hex(col.r)
    hex = hex .. num2hex(col.g)
    hex = hex .. num2hex(col.b)
    hex = hex .. num2hex(col.a)
    return hex
end

function hex2color(hex)
    local col = color(255, 255, 255, 255)
    col.r = tonumber(string.sub(hex, 1, 2), 16)
    hex = string.sub(hex, 3, string.len(hex))
    col.g = tonumber(string.sub(hex, 1, 2), 16)
    hex = string.sub(hex, 3, string.len(hex))
    col.b = tonumber(string.sub(hex, 1, 2), 16)
    hex = string.sub(hex, 3, string.len(hex))
    col.a = tonumber(string.sub(hex, 1, 2), 16)
    return col
end

@stevon8ter - I think it might be easier in that case just to store the colour numbers as a string and convert that back into a colour. It would still take some lines of code, but it would be a lot shorter.

function setup()
    saveLocalData("color-a","100 100 50 255") 
    a=ReadColor("color-a")   
end

function ReadColor(c)
    local u=readLocalData(c)
    local t={}
    for i in string.gmatch(u,"%S+") do
        table.insert(t,i)
    end
    return color(t[1],t[2],t[3],t[4])
end

function draw()
    background(a)
end

@Ignatz yeah that would probably be better, but I just wanted to point out the several options ;p

well, maybe this will come in handy in the future, giving people multiple examples on how to do the same thing is always learnfull and interesting :slight_smile:

Thanks!

@Mr_Ninja Don’t know if this would help. The colors could be stored in Global data instead of Local data, that way they would be read from any project. I’m only storing the first 21 colors in local data in this example. Once the colors are saved, then you don’t need all this code.


function setup()
    rgbTab = {
        aliceblue = {240, 248, 255},
        antiquewhite = {250, 235, 215},
        aqua = { 0, 255, 255},
        aquamarine = {127, 255, 212},
        azure = {240, 255, 255},
        beige = {245, 245, 220},
        bisque = {255, 228, 196},
        black = { 0, 0, 0},
        blanchedalmond = {255, 235, 205},
        blue = { 0, 0, 255},
        blueviolet = {138, 43, 226},
        brown = {165, 42, 42},
        burlywood = {222, 184, 135},
        cadetblue = { 95, 158, 160},
        chartreuse = {127, 255, 0},
        chocolate = {210, 105, 30},
        coral = {255, 127, 80},
        cornflowerblue = {100, 149, 237},
        cornsilk = {255, 248, 220},
        crimson = {220, 20, 60},
        cyan = { 0, 255, 255},
        darkblue = { 0, 0, 139},
        darkcyan = { 0, 139, 139},
        darkgoldenrod = {184, 134, 11},
        darkgray = {169, 169, 169},
        darkgreen = { 0, 100, 0},
        darkgrey = {169, 169, 169},
        darkkhaki = {189, 183, 107},
        darkmagenta = {139, 0, 139},
        darkolivegreen = { 85, 107, 47},
        darkorange = {255, 140, 0},
        darkorchid = {153, 50, 204},
        darkred = {139, 0, 0},
        darksalmon = {233, 150, 122},
        darkseagreen = {143, 188, 143},
        darkslateblue = { 72, 61, 139},
        darkslategray = { 47, 79, 79},
        darkslategrey = { 47, 79, 79},
        darkturquoise = { 0, 206, 209},
        darkviolet = {148, 0, 211},
        deeppink = {255, 20, 147},
        deepskyblue = { 0, 191, 255},
        dimgray = {105, 105, 105},
        dimgrey = {105, 105, 105},
        dodgerblue = { 30, 144, 255},
        firebrick = {178, 34, 34},
        floralwhite = {255, 250, 240},
        forestgreen = { 34, 139, 34},
        fuchsia = {255, 0, 255},
        gainsboro = {220, 220, 220},
        ghostwhite = {248, 248, 255},
        gold = {255, 215, 0},
        goldenrod = {218, 165, 32},
        gray = {128, 128, 128},
        grey = {128, 128, 128},
        green = { 0, 128, 0},
        greenyellow = {173, 255, 47},
        honeydew = {240, 255, 240},
        hotpink = {255, 105, 180},
        indianred = {205, 92, 92},
        indigo = { 75, 0, 130},
        ivory = {255, 255, 240},
        khaki = {240, 230, 140},
        lavender = {230, 230, 250},
        lavenderblush = {255, 240, 245},
        lawngreen = {124, 252, 0},
        lemonchiffon = {255, 250, 205},
        lightblue = {173, 216, 230},
        lightcoral = {240, 128, 128},
        lightcyan = {224, 255, 255},
        lightgoldenrodyellow = {250, 250, 210},
        lightgray = {211, 211, 211},
        lightgreen = {144, 238, 144},
        lightgrey = {211, 211, 211},
        lightpink = {255, 182, 193},
        lightsalmon = {255, 160, 122},
        lightseagreen = { 32, 178, 170},
        lightskyblue = {135, 206, 250},
        lightslategray = {119, 136, 153},
        lightslategrey = {119, 136, 153},
        lightsteelblue = {176, 196, 222},
        lightyellow = {255, 255, 224},
        lime = { 0, 255, 0},
        limegreen = { 50, 205, 50},
        linen = {250, 240, 230},
        magenta = {255, 0, 255},
        maroon = {128, 0, 0},
        mediumaquamarine = {102, 205, 170},
        mediumblue = { 0, 0, 205},
        mediumorchid = {186, 85, 211},
        mediumpurple = {147, 112, 219},
        mediumseagreen = { 60, 179, 113},
        mediumslateblue = {123, 104, 238},
        mediumspringgreen = { 0, 250, 154},
        mediumturquoise = { 72, 209, 204},
        mediumvioletred = {199, 21, 133},
        midnightblue = { 25, 25, 112},
        mintcream = {245, 255, 250},
        mistyrose = {255, 228, 225},
        moccasin = {255, 228, 181},
        navajowhite = {255, 222, 173},
        navy = { 0, 0, 128},
        oldlace = {253, 245, 230},
        olive = {128, 128, 0},
        olivedrab = {107, 142, 35},
        orange = {255, 165, 0},
        orangered = {255, 69, 0},
        orchid = {218, 112, 214},
        palegoldenrod = {238, 232, 170},
        palegreen = {152, 251, 152},
        paleturquoise = {175, 238, 238},
        palevioletred = {219, 112, 147},
        papayawhip = {255, 239, 213},
        peachpuff = {255, 218, 185},
        peru = {205, 133, 63},
        pink = {255, 192, 203},
        plum = {221, 160, 221},
        powderblue = {176, 224, 230},
        purple = {128, 0, 128},
        red = {255, 0, 0},
        rosybrown = {188, 143, 143},
        royalblue = { 65, 105, 225},
        saddlebrown = {139, 69, 19},
        salmon = {250, 128, 114},
        sandybrown = {244, 164, 96},
        seagreen = { 46, 139, 87},
        seashell = {255, 245, 238},
        sienna = {160, 82, 45},
        silver = {192, 192, 192},
        skyblue = {135, 206, 235},
        slateblue = {106, 90, 205},
        slategray = {112, 128, 144},
        slategrey = {112, 128, 144},
        snow = {255, 250, 250},
        springgreen = { 0, 255, 127},
        steelblue = { 70, 130, 180},
        tan = {210, 180, 140},
        teal = { 0, 128, 128},
        thistle = {216, 191, 216},
        tomato = {255, 99, 71},
        turquoise = { 64, 224, 208},
        violet = {238, 130, 238},
        wheat = {245, 222, 179},
        white = {255, 255, 255},
        whitesmoke = {245, 245, 245},
        yellow = {255, 255, 0},
        yellowgreen = {154, 205, 50}
    }
    -- create color keys and color values
    -- save them in local data
    -- could be saved in global data
    for a,b in pairs(rgbTab) do
        str=string.format("%d,%d,%d",rgbTab[a][1],rgbTab[a][2],rgbTab[a][3])
        saveLocalData(a,str)
    end
end
    
function draw()
    background(255)
    getCol("aqua")  -- set the fill color
    text("this is aqua color",200,400)
    getCol("blue")
    text("this is blue color",200,350)
    getCol("burlywood")
    text("this is burlywood color",200,300)
    getCol("crimson")
    text("this crimson color",200,250)
end

function getCol(c)
    r,g,b=string.match(readLocalData(c),"(%d+),(%d+),(%d+)")  
    fill(r,g,b)    
end

The user and all related content has been deleted.

@Ignatz It can actually be done in one line each.

-- Color String Saving

function setup()
    col = color(255, 127, 63, 195)
    print(col)
    txt = colorToText(col)
    print(txt)
    col = textToColor(txt)
    print(col)
end

function colorToText(col)
    return string.match(tostring(col), "%((.-)%)") .. ","
end

function textToColor(txt)
    return color(string.match(txt, "(%d-), (%d-), (%d-), (%d-),"))
end

function draw() 
    background(col)
end

How would I convert a Color data to a string? Is there a function to do that? I haven’t been able to find it

@Mr_Ninja, @SkyTheCoder has just posted a function for that in the post above :wink:
(colorToText)

Thanks, didn’t see that.

That’s not exactly what I’m looking for. That requires you to use both functions, and im just wondering if for using Ignatz method how would you save a Color data to the local data that you read (ie saveLocalData(“col1”, “col1=”…–self.col1 data here) The reason I’m doing this is because I’m using themes in an app I’ve been working on for a month or so, and im planning on releasing a video soon.

EDIT: figured it out myself

saveLocalData("col1","col1=color("..tostring(self.col1.r)..","..tostring(self.col1.g)..","..tostring(self.col1.b)..","..self.col1.a..")")

Yes, you shouldn’t need tostring, Codea can figure that out itself

@Mr_Ninja, @se24vad, @SkyTheCoder, @dave1707 - I thought this discussion would make a nice post, so that’s what I did, here

http://coolcodea.wordpress.com/2014/09/27/167-a-couple-of-useful-lua-tips/

I think I was also able to improve on the solutions suggested here, too - but I claim no credit for that whatsoever - all the good ideas came from you guys.

My final suggestions are

[1] match option
--save color col
saveLocalData("myColour",tostring(col)) 
--Load it back into col2 
local strCol=readLocalData("myColour") 
col2 = color(string.match(strCol, "(%d-), (%d-), (%d-), (%d-)%)"))

[2] loadString option
--save color col
saveLocalData("myColour","return color"..tostring(col)) 
--read colour
col2=loadstring(readLocalData("myColour"))()

i followed the link and started to read some of your excellent recent posts.
I have noticed that the code ‘example of using noise to create rolling hills’ constantly kills codea when i play with parameters. Is it the same for you?