Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Storing/Retrieving Color from Text File  RSS feed

 
Joe Pettit
Ranch Hand
Posts: 33
C++ Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks, wasn't sure which forum would be best for this, so apologies if this is off topic

I'm trying to store a color data in a text file. I understand that it will be converted to a string, but when I go to read that information again and bring it into memory, what would be the best way to convert this string back to a color? I'm storing the data in this format:

java.awt.Color[r=0,g=255,b=51]

Is there a better way to encode this information? If so, how would I go about changing it? I'm using the default color chooser right now for the user to select a color.

Not sure if its relevant, but here's the code I'm working with to read the color information from the file:



Thanks all!
 
Rishi Shah
Ranch Hand
Posts: 43
Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can store color as a single int value. For example:



This will lead to painless storage, reconversion, and smaller files. You can restore it as a color by simply:



This is very strait-forward if you understand bitwise operations. If not, feel free to ask.
 
Joe Pettit
Ranch Hand
Posts: 33
C++ Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rishi Shah wrote:

This is very strait-forward if you understand bitwise operations. If not, feel free to ask.


Thanks for that super-speedy response Rishi! Can't say I'm too well versed in bitwise operations - is there a nice tutorial out there for this? Thanks again!
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of using a Color[] and reading the file twice, you should use a List<Color> and use only one loop:
 
Joe Pettit
Ranch Hand
Posts: 33
C++ Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Instead of using a Color[] and reading the file twice, you should use a List<Color> and use only one loop:


I hadn't considered that Rob, thanks, I'll implement that now.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
Joe Pettit
Ranch Hand
Posts: 33
C++ Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:You're welcome.


Any ideas how I can store the color info in a more accessible format?
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the alpha value is irrelevant you could HEX encode it, in RRGGBB format (possibly with a # in front of it, like used in HTML and CSS). Converting shouldn't be too hard.
 
Rishi Shah
Ranch Hand
Posts: 43
Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:If the alpha value is irrelevant you could HEX encode it, in RRGGBB format (possibly with a # in front of it, like used in HTML and CSS). Converting shouldn't be too hard.


Doing it the way I showed him above would preserve the alpha value, and the conversion is very easy.

Joe Pettit wrote:
Rishi Shah wrote:

This is very strait-forward if you understand bitwise operations. If not, feel free to ask.


Thanks for that super-speedy response Rishi! Can't say I'm too well versed in bitwise operations - is there a nice tutorial out there for this? Thanks again!


http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rishi Shah wrote:
Rob Spoor wrote:If the alpha value is irrelevant you could HEX encode it, in RRGGBB format (possibly with a # in front of it, like used in HTML and CSS). Converting shouldn't be too hard.


Doing it the way I showed him above would preserve the alpha value, and the conversion is very easy.

True. Instead of storing it in RRGGBB format, AARRGGBB could (and should) be used instead.
 
Joe Pettit
Ranch Hand
Posts: 33
C++ Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rishi Shah wrote:
Rob Spoor wrote:If the alpha value is irrelevant you could HEX encode it, in RRGGBB format (possibly with a # in front of it, like used in HTML and CSS). Converting shouldn't be too hard.


Doing it the way I showed him above would preserve the alpha value, and the conversion is very easy.

Joe Pettit wrote:
Rishi Shah wrote:

This is very strait-forward if you understand bitwise operations. If not, feel free to ask.


Thanks for that super-speedy response Rishi! Can't say I'm too well versed in bitwise operations - is there a nice tutorial out there for this? Thanks again!


http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html


Thanks again Rishi - I'll take a look and see if I can modify it to work using what you suggested. In the meantime, I was able to get it to work with a tokenizer - I'm sure this is not the preferred way of doing it, but it appears to work consistently.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!