Drawing smooth lines without changing color due to background?

All the smoothed line drawing algorithms I came across simply say that the "intensity" of the pixels should be a function of how much of the line passes through it. This works great against a constant background (i.e. white), but I want to be able to paint against a background of arbitrary complexity, which means replacing the intensity with transparency and alpha blending the line with the background.

Doing this necessarily changes the color of the line depending on what the background is, since for a 1px line it rarely passes exactly through one pixel, giving it full opacity. I am curious if there is a technique for drawing these mixed lines, while maintaining the appearance of the original color.

Here is an example of my rendering attempt on a colored background. You will notice that the vertical / horizontal lines are drawn as a special case with real color, and the smoothed diagonal lines have a blue tint.

enter image description here

Is there a suitable way to blend smoothed lines in the background while keeping the appearance of the correct line color?

Edit: and code for actually plotting points:

// Plot pixel at (x,y) with color at transparency alpha [0,1] static inline void plot(pixel_t *pixels, uint16_t stride, const pixel_t &color, uint16_t x, uint16_t y, uint8_t alpha) { pixel_t pix = pixels[y*stride+x]; pixels[y*stride+x].r = (uint16_t)color.r * alpha/255 + pix.r * (255 - alpha) / 255; pixels[y*stride+x].g = (uint16_t)color.g * alpha/255 + pix.g * (255 - alpha) / 255; pixels[y*stride+x].b = (uint16_t)color.b * alpha/255 + pix.g * (255 - alpha) / 255; } 

Edit: For future generations, mixing green and blue can lead to blue hues.

+4
source share
1 answer

I'm glad you noticed an error in your code.

Another issue that needs to be addressed is gamma correction. Smoothing should be applied in a linear color space in order to look correct, but in most cases it is used in a gamma-corrected color space to preserve some processing steps. The effects are much more subtle than your example.

+2
source

Source: https://habr.com/ru/post/1413463/


All Articles