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 & HelpSyntax Questions › Manipulating Color Datatype
Page Index Toggle Pages: 1
Manipulating Color Datatype (Read 682 times)
Manipulating Color Datatype
Aug 26th, 2008, 6:24am
 
Hi,

I would just like a confirmation on a problem I'm wrestling with. I have an array of type color. From searching around, it seems that there are no functions available for modifying the ARGB info once it is in the color variable. For example if I want to add or subtract red, I have to pull out individually the ARGB components into separate variables, make the modification(s), and then stick them all back into the color variable. Is this correct?

Thanks.

Jim
Re: Manipulating Color Datatype
Reply #1 - Aug 26th, 2008, 8:21am
 
Yes, that's correct. Of course, you can do these functions yourself.
Re: Manipulating Color Datatype
Reply #2 - Aug 26th, 2008, 2:47pm
 
Hi PhiLho,

Thanks for confirming. I was hoping to be able to avoid doing this myself. Seems like it could be useful to add functions for manipulating the ARGB components into Processing itself.

I suppose that I could write functions that would take arguments for color and 1-4 of the color components and then figure out how to do bitwise or's on the color value.

Given the size of the color arrays I anticipate having, I was really hoping for something more efficient than what I could produce.

Jim
Re: Manipulating Color Datatype
Reply #3 - Aug 26th, 2008, 4:50pm
 
unfortunately color isn't actually a datatype.. it's just a synonym for int behind the scenes, hence why there are no built in methods for it.
Re: Manipulating Color Datatype
Reply #4 - Aug 26th, 2008, 6:26pm
 
You can convert between Processing's use of integers to represent colours and Java's own Color class though. That then allows you to manipulate the individual components without resorting to bit manipulation. It also gives you access to Java's colour methods such as conversion to HSB, interpretation of HTML style hex colours etc.

A simple example:

Quote:
int myCol1, myCol2, myCol3;

void setup()
{
  size(400,400);
  smooth();
  
  // Original Processing colour.
  myCol1 = color(200,150,50,255);
  
  // Create a Java Color object from Processing colour.
  Color javaCol = new Color(myCol1);
      
  // Darken Java colour and then convert to processing colour.
  myCol2 = javaCol.darker().getRGB();

  // Create processing colour with boosted green from Java colour components.
  int r=javaCol.getRed();
  int g=javaCol.getGreen();
  int b=javaCol.getBlue();
  int a=javaCol.getAlpha();
  myCol3 = color(r/2,g,b/2,a);  
}

void draw()
{
  background(255);
  
  fill(myCol1);
  ellipse(width/2,height*.25,100,100);
  fill(myCol2);
  ellipse(width/2,height*.5,100,100);
  fill(myCol3);
  ellipse(width/2,height*.75,100,100);
}



Re: Manipulating Color Datatype
Reply #5 - Aug 26th, 2008, 6:33pm
 
As pointed out, color is just an int, so if Processing provided functions to set an individual component, it wouldn't be more efficient than those you would write.
Actually, these functions could even be slower: see how blue() (for example) is slower than direct bit access, because of additional treatment.

Quick, rough implementation:
Code:
color setBlue(color c, int b)
{
return (c & 0xFFFFFF00) | b;
}
color setGreen(color c, int g)
{
return (c & 0xFFFF00FF) | (g << 8);
}
color setRed(color c, int r)
{
return (c & 0xFF00FFFF) | (r << 16);
}
color setAlpha(color c, int a)
{
return (c & 0x00FFFFFF) | (a << 24);
}

void setup()
{
color c = color(50, 100, 150);
c = setBlue(c, 128);
println(hex(c));
c = setGreen(c, 128);
println(hex(c));
c = setRed(c, 128);
println(hex(c));
c = setAlpha(c, 128);
println(hex(c));
}
Re: Manipulating Color Datatype
Reply #6 - Aug 26th, 2008, 9:02pm
 
Hi Jo,

I was writing in reference to the Color class. Looking through the associated methods at:

http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Color.html

you will see that there are numerous methods provided for retrieving the color information, but there are no methods provided for modifying the information within that color object.

For example, an alterGreen(int valu) method where I could either increase or decrease the amount of green would be really nice. Same for each of the other components.

PhiLho: thanks for the sample code. On the plus side, anything I come up with for this particular sketch would be useful in similar situations in other sketches.

Speaking of which, is anyone aware of any libraries that may have been developed which contain this sort of functionality?

Thanks, Jim
Re: Manipulating Color Datatype
Reply #7 - Aug 27th, 2008, 4:44pm
 
Ah, I was mistaken.
Well, Color objects, like the String one, for example, is immutable, which is seen as good design nowadays.
Ie. you don't change such object, but create a new object with given change (like darken and brighten methods do).
If you want a mutable object, I fear you have to write your own class.
Page Index Toggle Pages: 1