Android Question what is the best way to load colors from a file

Cableguy

Expert
Licensed User
Longtime User
Hi guys

What is the best way to deal with reading/writing color values from/to a file?
I recon the best approach is to convert them to integers before writing and to use them as integers when reading... what do you usually do?
 

Peter Simpson

Expert
Licensed User
Longtime User
Sounds good to me, then you can save them to a .dat RandomAccessFile.
Hmm but saying that, I save each R, G, B value separately for my TIX Clock widget https://play.google.com/store/apps/details?id=com.simplysoftware.tixclock before saving the values in the file. I then read the separate R, G, B values when the widget first starts and I put the values back into their original places.

DAAAAAAAAAAAAAAAM, I've just realised that you're member number four on this great community. What, did you join up the same day the site was released online???

Why am I attempting to answer your question for, you're an expert. Ignore what I said about my app ;)
 
Upvote 0

ivan.tellez

Active Member
Licensed User
Longtime User
It deppends on the amount of data to be store and frequency to be readed.

For simple apps, saving INTs on a file should be enought.

To store an read more colors, maybe SQLite.

And for LOTS of colors to be readed, its better to use a data structure, Serialize and Deserialize to stored it in a file, or as a BLOB in a database.

For this step you can use JSON to serializa and in memory compression to optimize writing and reading times.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
DAAAAAAAAAAAAAAAM, I've just realised that you're member number four on this great community. What, did you join up the same day the site was released online???

Why am I attempting to answer your question for, you're an expert. Ignore what I said about my app ;)

LoL... Actually I think I registered within the first 2 hours of the extinct b4ppc.com release. But I was around before that, back when Erel was just starting to get b4ppc noticed by the masses, and the b4ppc forum was offered us by a comunity member called GeoTrail.
I may be labeled as an Expert, but I am way more close to a noob than an Expert.

Now, coming back to my question...
What I plan to do is to allow theming an app I'm planning for my wife (you know how girls love to change things around), giving her the ability to change background and border colors of the apps views. The file will then be stored inside a theme folder, and only read once on app start...

Dividing the colors in to its R;G;B components seems to be cumbersome to me... if I understand correctly, colors.RGB only converts the given components to an integer, so I can just feed this integer as a cor value.

I was thinking on using either an array or map to store the color values... but I will check the KeyStore...
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Colors are integers. You don't need to do anything special to convert a color to int.

SS-2015-02-17_13.12.08.png
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Hey @Cableguy, you were an early adapter of b4ppc, good on you :)
Yes they are integers thus can be stored simply enough, in my case all my colours can be user customised, I just stored them separately as I'm using ColorDialog and I just wanted to do it that way. If my memory serves me correctly, cont all the colour integers start with a (-)hyphen. I remember doing ToastMessageShow(Colors.Black, False) and ToastMessageShow(Colors.RGB(0, 0, 0), False) thinking that it would return 000000 or 0, but infact it returned some strange -16xxxx number, so I saved the RGB values separately instead, that was just me though.

@Erel you have told user a few times now to use the KeyValueStore Class when storing values. I've never ever looked into that class so maybe on my next project I will look into that class, but I personally like storing values in an encrypted RandomAccessFile. you obviously know more about these things than anybody else as you wrote them. I will have to look into KeyValueStore to see what the benefits are compared to using RandomAccessFile if any.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Hey @Cableguy, you were an early adapter of b4ppc, good on you :)
Yes they are integers thus can be stored simply enough, in my case all my colours can be user customised, I just stored them separately as I'm using ColorDialog and I just wanted to do it that way. If my memory serves me correctly, cont all the colour integers start with a (-)hyphen. I remember doing ToastMessageShow(Colors.Black, False) and ToastMessageShow(Colors.RGB(0, 0, 0), False) thinking that it would return 000000 or 0, but infact it returned some strange -16xxxx number.....
SInce I will be establishing the themes myself, I may take the easy route and just write the output of colors.rgb (xxx,xxx,xxx)
You should also keep in mind when dealing with colors, that we usually use solid colors, but code wise, they just have an alfa of 255!
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Dam it of course I never even though about the Alpha being defaulted to 255, that's obviously why. Obviously using Colors.ARGB and setting the Alpha to 0 will bring back 0 as I originally expected. I just remember trying RGB 0, 0, 0 and not getting 0 in return, but in return getting -16xxxx integer. So I just saved the RGB values separately as I was in a hurry to finish the app and didn't have time to look into it fully. Anyway I'm pleased that I did it that way as I learned about other things instead :).

Wow you learn something new every single day on this great community, and as I stated earlier @Cableguy.
Why am I attempting to answer your question for, you're an expert. Ignore what I said about my app

Sorry :p
 
Upvote 0
Top