0

If I was given color A and color B, how can one go aboit generating a gradient on a canvas which can be later converted to a bitmap.

Such that

public Bitmap makeGradient(Color from, Color to){}

Would actually work?

I hope this is not too vague. I thankyou for your time and effort.

Ps. There is a question on stackoverflow that answers this but I amstill confused :(

Here it is: Generating gradients programmatically?

Community
  • 1
  • 1
Mathew Kurian
  • 5,949
  • 5
  • 46
  • 73
  • Please post a link to the other question you found and explain which part confuses you – simchona Jan 13 '12 at 07:46
  • http://stackoverflow.com/questions/27532/generating-gradients-programatically – Mathew Kurian Jan 13 '12 at 07:47
  • Im tryng to create a radial gradient with a focus. Android doesnt seem to have one built in. link to.my question in Android section http://stackoverflow.com/questions/8651810/radial-gradient-but-with-a-focus – Mathew Kurian Jan 13 '12 at 07:51

1 Answers1

1

One way to go about creating a radial gradient might be to define the focus point as well as the extent of the gradient and when you generate the image you'd calculate the distance between the current pixel and the focus point, divide it by the gradient extent and clip the result to 1. Then use the formula in the question you linked.

Something like this pseudocode:

double d = distance(currentPixel, focusPoint); //I'll leave the implementation for you
double factor = Math.max(1.0, d/extent);

int red = (int) (firstCol.getRed() * factor + secondCol.getRed() * (1.0 - factor) );
int green= (int) firstCol.getGreen() * factor + secondCol.getGreen()* (1.0 - factor) );
int blue = (int) (firstCol.getBlue() * factor + secondCol.getBlue()* (1.0 - factor) );

This would mean that the farther a pixel is from the focus point the more firstCol will contribute to it (pixels that are outside the extent of the gradient will only use firstCol since factor should be 1.0 for those).

Thomas
  • 87,414
  • 12
  • 119
  • 157
  • Im confused on how i paint this. because the focus can causes the normal radial gradient to be rather skewed so I can't set a clip/rect to paint it in. – Mathew Kurian Jan 13 '12 at 16:06
  • @bluejamesbond I may have misunderstood you but isn't the focus point the center of the gradient. And don't you draw to an image which normally is rectangular? In those cases you would just iterate over the pixels and calculate the gradient color for each of them. If I misunderstood you, please clarify (maybe by posting an image of what you want to achieve). – Thomas Jan 13 '12 at 16:11
  • I meant something like this http://stackoverflow.com/questions/8651810/radial-gradient-but-with-a-focus (I had posted this previously in the android forums but no one answered) – Mathew Kurian Jan 13 '12 at 18:05
  • 1
    @bluejamesbond It looks like you want to create a normal radial gradient and then project that onto another plane whose normal would be the normalized version of vector (dx,dy,1) where dx is the distance between the gradient center and the focal point on the x axis and dy is the distance on the y axis. I'd have to look up plane-plane projections, so you might do that as well. You'll most likely find 3D algorithms but since you work in 2D you might be able to simplify the equations (much like I simplified the normal vector, since z is 1 in the 2D case). – Thomas Jan 13 '12 at 21:25
  • ill look into it a bit more and then message you if i have any doubts. – Mathew Kurian Jan 13 '12 at 22:00
  • I hope you can still help me. Basically, the gradient I want to create is something like this http://www.graphicxtras.com/images/radial-gradients-illustrator.jpg If you look on the 2nd column, there are some good examples. This doesn't have to be in 3D areas...just 2D :D I am really confused as to what the method distance(...) does in your code. – Mathew Kurian Jan 26 '12 at 20:36
  • @bluejamesbond distance would basically return the distance between two points (you might return that in pixels, e.g. the distance of two pixels next to each other would be 1, the distance of two pixels sharing one corner (they are diagonal) would be sqrt(2), etc. etc. – Thomas Jan 26 '12 at 20:43
  • with each color received do i paint each pixel separately? – Mathew Kurian Jan 26 '12 at 20:45