Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have read up on bitwise operators (& | ^) and I understand that if I were to do:

alpha = 0xFF000000 >> 24 ;
blue = 0xFF0000FF & 0x000000FF;
red = 0xFFFF0000>>16 & 0x000000FF;
green = 0xFF00FF00>>8 & 0x000000FF;

then I can mask the other colors and just have red or blue(etc...) components and if I were to do

int color = alpha | blue | red | green;

then I re-construct the color again so to speak. What I am curious about is what if I wanted to create a bilinear interpolation between two colors in Java. How would I go about constructing it? I would like to start with the standard green color( 0xFF00FF00) and end with black (0xFF000000), the colors in the middle would be change from green to darker greens until it eventually gets to black. I think that I would have to do something where I create a bufferedImage which starts off as green at the top and then maybe create a for loop that would read the color of the previous pixel and then shift something until a new version of the previous color is created and so forth. Unfortunately I am not sure how to implement this because I understand bitwise operations and shifts in theory but am not sure how to apply them for this purpose. Any help would be greatly appreciated! Thank you in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
361 views
Welcome To Ask or Share your Answers For Others

1 Answer

As EJP noted the masks are used to filter out the values for one of the color components/channels (Red, Green, Blue). And that's usually what you do with bit shift/masking operations. Everything else you do with the values you get is arithmetics or more advanced math.

A color channel's range is 0-255, or 0x00 - 0xFF in hexadecimal values. To reconstruct it, you need to bitshift the components value back to their place. Which can be put together with simple arithmetic addition:

// Example values
int r = 255; // 0xFF
int g = 1;   // 0x01
int b = 15;  // 0x0F

// go back to original form:
                      //    A  R  G  B
int color = r << 16;  // 0x00.FF.00.00
color += g << 8;      // 0x00.FF.01.00
color += b;           // 0x00.FF.01.0F

// just add back the alpha if it is going to be full on
color = += 255 << 24; // 0xFF.FF.01.0F

If you want to do some interpolation between colors you need to do it for each color component seperately, not all of them together in one integer. In some instances it may also a good idea to change the representation from [0-255] to a decimal one [0.0f-1.0f]:

// Darken red value by 50%
int color = ...; // some color input
int mask = 0xFF;

int a = (color >> 24) & mask;
int r = (color >> 16) & mask;
int g = (color >> 8) & mask;
int b = color & mask;

// convert to decimal form:
float rDecimal = r / 255f; 
  // Let r: 0x66 = 102 => rDecimal: 0.4

// darken with 50%, basically divide it by two
rDecimal = r/2; 
  // rDecimal: 0.2

// Go back to original representation and put it back to r
r = (int)(rDecimal * 255); 
  // r: 51 = 0x33

// Put it all back in place
color = (a << 24) + (r << 16) + (g << 8) + b;

Hope this helps out.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...