20084

smooth color interpolation along a “bresenham” line

I am trying to interpolate color along a line so that, given two points and their respective RGB values, I can draw a line with a smooth color gradient. Using Bresenham's Line Algorithm, I can now draw lines, but am not sure how to begin interpolating colors between the two end points. The following is part of the drawLine() function that works for all line whose slope are less than 1.

int x_start = p1.x, x_end = p2.x, y_start =p1.y, y_end = p2.y; int dx = Math.abs(x_end-x_start), dy = Math.abs(y_end-y_start); int x = x_start, y = y_start; int step_x = x_start < x_end ? 1:-1; int step_y = y_start < y_end ? 1:-1; int rStart = (int)(255.0f * p1.c.r), rEnd = (int)(255.0f * p2.c.r); int gStart = (int)(255.0f * p1.c.g), gEnd = (int)(255.0f * p2.c.g); int bStart = (int)(255.0f * p1.c.b), bEnd = (int)(255.0f * p2.c.b); int xCount = 0; //for slope < 1 int p = 2*dy-dx; int twoDy = 2*dy, twoDyMinusDx = 2*(dy-dx); int xCount = 0; // draw the first point Point2D start = new Point2D(x, y, new ColorType(p1.c.r, p1.c.g, p1.c.b)); drawPoint(buff, start); float pColor = xCount / Math.abs((x_end - x_start)); System.out.println(x_end + " " + x_start); while(x != x_end){ x+= step_x; xCount++; if(p<0){ p+= twoDy; } else{ y += step_y; p += twoDyMinusDx; } Point2D draw_line = new Point2D(x, y, new ColorType(p1.c.r*(1-pColor)+p2.c.r*pColor,p1.c.g*(1-pColor)+p2.c.g*pColor,p1.c.b*(1-pColor)+p2.c.b*pColor)); System.out.println(pColor); drawPoint(buff,draw_line ); }

So what I'm thinking is that, just like drawing lines, I also need some sort of decision parameter p to determine when to change the RGB values. I am thinking of something along lines of as <strong>x increments, look at each rgb value and decide if I want to manipualte them or not</strong>.

I initialized rStart and rEnd(and so on for g and b) but have no idea where to start. any kind of help or suggestions would be greatly appreciated!

Edit: thanks @Compass for the great suggestion ! Now I've ran into another while trying to implementing that strategy, and I am almost certain it's an easy bug. I just can't see it right now. For some reason my <strong>pColor</strong> always return 0, I am not sure why. I ran some print statements to make sure xCount is indeed increasing, so I am not sure what else might've made this variable always 0.

Answer1:

I remember figuring this out way back when I was learning GUI! I'll explain the basic concepts for you.

Let's say we have two colors, RGB(A,B,C) and RGB(X,Y,Z) for simplicity.

If we know the position percentage-wise (we'll call this P, a float 0 for beginning, 1.0 at end) along the line, we can calculate what color should be there using the following:

Resultant Color = RGB(A*(1-P)+X*P,B*(1-P)+Y*P,C*(1-P)+Z*P)

In other words, you average out the individual RGB values along the line.

Answer2:

Actually you will be drawing the line in RGB space as well !

Bresenham lets you compute point coordinates from (X0, Y0) to (X1, Y1).

This is done by a loop on X or Y, with a linear interpolation on the other coordinate.

Just extend the algorithm to draw a line from (X0, Y0, R0, G0, B0) to (X1, Y1, R1, G1, B1), in the same loop on X or Y, with a linear interpolation on the other coordinates.

Recommend

  • Put a image in a circle view
  • Force Close with AdMob; developing for android 2.2
  • This convoluted generics pattern crashes Eclipse - can I make it work?
  • SQL: Group counts in individual result rows
  • Adding an incrementing value attribute to every tag in xml using script
  • Strange exception
  • opencv fitLine always crashes
  • Call same SUB from multiple buttons in VBA
  • VexCL vector of structs?
  • Override Data Tip Circle
  • c++ search a vector for element first seen position
  • How to save dynamically created textboxes and their values
  • Access the state of control in Winforms from another application
  • Can a Collections.shuffle be considered equivalent to a series of Randoms?
  • Creating a setup.exe in InstallShield in Visual Studio
  • Draw half infinite lines?
  • Guava how to copy all files from one directory to another
  • XOR with Neural Networks (Matlab)
  • How to rotate the dial in a gauge chart? Using python plotly
  • In Java, how can I construct a File from a resource?
  • JavaScript IE rotation transform maths
  • Plotting densities in R
  • How load cv2.KeyPoint and Descriptors correctly on OpenCV 3 with Python 3?
  • How to create CGPath from a SKSpriteNode in SWIFT
  • Z3: Convert between FP and BitVector?
  • Using a canvas object in a thread to do simple animations - Java
  • uniform generation of points on 3D box
  • What does 'Language neutral' mean with regard to MAKELANGID?
  • Visual Studio 2010 debugger build correctly - compiler pdb and linker pdb not in synch?
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • How to use remove-erase idiom for removing empty vectors in a vector?
  • Highlight one bar in a series in highcharts?
  • Android screen density dpi vs ppi
  • How would I use PHP exceptions to define a redirect?
  • How to extract text from Word files using C#?
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • python regex in pyparsing
  • need help with bizarre java.net.HttpURLConnection behavior
  • Android Google Maps API OnLocationChanged only called once
  • How can I use threading to 'tick' a timer to be accessed by other threads?