We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Question about Color (HEX) values! Rotate/Shift
Page Index Toggle Pages: 1
Question about Color (HEX) values! Rotate/Shift? (Read 1596 times)
Question about Color (HEX) values! Rotate/Shift?
Jan 4th, 2010, 7:43am
 
Hello dear users,

I'm actually coding a little program with processing.

My problem is, that I have a color-bar. I choose some color out of this bar with my left mouse button.

This value is saved in a variable of the "color" type with this function:

Code:
color value = get(mouseX, mouseY); 



Now I'm able to make an output of this selected hex-value:

Code:
println("Original Hex Value: " + hex(value));  



gives this back: Original Hex Value: FF4700FF

I don't really know, why i go 8 characters back, because the important value is just "4700FF". THIS is the hex value of the color i selected.

Now I try to describe what I want to do with this value.

For example:
Original Hex Value: #4700FF

I want to "modify" this value with some kind of "right rotation".

The first modified value should look this this: #FF4700 and the second one #00FF47.

I need this operations to get a "Triadic Color Harmony" but I'm not able to do this "rotation step".

I tried already the following things:

1. I made a new variable of String type:  
Code:
String hex = "#"+hex(value,6); 



2. Made a new String out of the the original value:
Code:
String rotatedHex = hex.substring(5) + hex.substring(1,5); 



3. Did a String to Integer convert:
Code:
komp_wert1 = (Integer.valueOf(rotatedHex,16).intValue()); 



4. Some println():
Code:
  println("Original Hex: " + hex(value)); 
 println("OriginalHex as String: " + hex);
 println("Rotierted  Hex: " + rotatedHex);


5. Terminal output:
Code:
Original Hex: FFB700FF
Original Hex as String: #B700FF
Rotierted Hex: FFB700


Can anybody bring me a little light to solve this problem?

Thanks in advance!
Mike





Re: Question about Color (HEX) values! Rotate/Shift?
Reply #1 - Jan 4th, 2010, 8:03am
 
Shifting hex colors... interesting!

Here is an attempt using the substring() method (see Javadoc for more info). Looks ugly, but works. Smiley

Code:
String s = "AABBCC";

String shiftLeft(String s) {
 return s.substring(2, 6) + s.substring(0, 2);
}

String shiftRight(String s) {
 return s.substring(4, 6) + s.substring(0, 4);
}

void setup() {
 println(s);
 println(shiftLeft(s));
 println(shiftRight(s));
}

Re: Question about Color (HEX) values! Rotate/Shift?
Reply #2 - Jan 4th, 2010, 8:25am
 
Mike85 wrote on Jan 4th, 2010, 7:43am:
Now I'm able to make an output of this selected hex-value:

Code:
println("Original Hex Value: " + hex(value));  



gives this back: Original Hex Value: FF4700FF

I don't really know, why i go 8 characters back, because the important value is just "4700FF".


The first two characters represent the alpha value of the colour; where FF = fully opaque and 00 = fully transparent.

antiplastik - just thinking aloud, but does this really require string manipulation?  I'm thinking it could be done with bitwise operators; though actually I suppose string manipulation might be simpler Wink
Re: Question about Color (HEX) values! Rotate/Shift?
Reply #3 - Jan 4th, 2010, 8:44am
 
Nice idea,

but the problem is, that i can not convert a String like "FFFFCC07" back to an Integer/Color Type.

For example:

Code:
println("Original Hex: " + hex(value));  


does this:
---> Original Hex: FF3F00FF

Code:
String hex = "#"+hex(value);
println("Original Hex als String: " + hex);

puts this from INT to HEX converted String to a new String named "hex"
--->Original Hex as String: #FF3F00FF



Code:
  String rotatedHex = hex.substring(1,3) +  hex.substring(7)  + hex.substring(3,7); 


gives back:
---> Rotated Hex: FFFF3F00

Now I have a String with the correct Hex value (from FF3F00FF to FFFF3F00).

Now i wan't to convert it back from String to Integer (because i need this to be able to use this color in my programm)

with this code:
Code:
int komp_wert1 = (Integer.valueOf(rotatedHex,16).intValue()); 



but I always get the following error code:
NumberFormatException: For input String: "FFFF3F00"

I don't know what to do Sad





Re: Question about Color (HEX) values! Rotate/Shift?
Reply #4 - Jan 4th, 2010, 10:23am
 
blindfish wrote on Jan 4th, 2010, 8:25am:
antiplastik - just thinking aloud, but does this really require string manipulation  I'm thinking it could be done with bitwise operators;


No doubt about it! That's what I meant when I said "Looks ugly, but works."  Grin
Re: Question about Color (HEX) values! Rotate/Shift?
Reply #5 - Jan 4th, 2010, 10:26am
 
Mike85 wrote on Jan 4th, 2010, 8:44am:
Now i wan't to convert it back from String to Integer (because i need this to be able to use this color in my programm)

with this code:
Code:
int komp_wert1 = (Integer.valueOf(rotatedHex,16).intValue()); 



Have a look at the reference :
http://processing.org/reference/unhex_.html

Code:
color c = unhex(rotatedHex); 


Re: Question about Color (HEX) values! Rotate/Shift?
Reply #6 - Jan 4th, 2010, 10:38am
 
I was just about to suggest unhex but my test isn't doing what I expect:

Code:
String s = "00FF00";

void setup() {
 size(100,100);
 // background should be #ff0000 = red
 background(shiftLeft(s));

 // rect should be #0000ff = blue, but it looks like it ignores the leading zeros and returns #ff
 fill(shiftRight(s));
 rect(25,25,50,50);
 
}


color shiftLeft(String s) {
 color c = unhex(s.substring(2, 6) + s.substring(0, 2));
 return c;
}

color shiftRight(String s) {
 color c = unhex(s.substring(4, 6) + s.substring(0, 4));
 return c;
}


It appears to be ignoring leading zeros when converting to the int, which doesn't seem right...  Unless of course I've done something wrong Wink
Re: Question about Color (HEX) values! Rotate/Shift?
Reply #7 - Jan 4th, 2010, 11:29am
 
antiplastik wrote on Jan 4th, 2010, 10:26am:
Mike85 wrote on Jan 4th, 2010, 8:44am:
Now i wan't to convert it back from String to Integer (because i need this to be able to use this color in my programm)

with this code:
Code:
int komp_wert1 = (Integer.valueOf(rotatedHex,16).intValue()); 



Have a look at the reference :


Code:
color c = unhex(rotatedHex); 




This was the right thing I looked for! Now it works fine!

Thanks to all the poster in this thread! Great community at all!
Re: Question about Color (HEX) values! Rotate/Shift?
Reply #8 - Jan 4th, 2010, 11:32am
 
I'm not allowed to post hyperlinks in this forum, because I have < 5 postings at the moment -.-!

Here is the latest version of my program:

hxxp://campus.fh-worms.de/~inf456/applet/index.html

(I have to substitute http ---> hxxp to post this link).

Just klick within the color-bar and you see the effect Wink
Page Index Toggle Pages: 1