Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rtextures] fix bad behaviour of GenImageGradientLinear with certain angle values. #4462

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/rtextures.c
Original file line number Diff line number Diff line change
Expand Up @@ -827,16 +827,27 @@ Image GenImageGradientLinear(int width, int height, int direction, Color start,
float cosDir = cosf(radianDirection);
float sinDir = sinf(radianDirection);

// Calculate how far the top-left pixel is along the gradient direction from the center of said gradient
float startingPos = 0.5 - (cosDir*width/2) - (sinDir*height/2);
// With directions that lie in the first or third quadrant (i.e. from top-left to
// bottom-right or vice-versa), pixel (0, 0) is the farthest point on the gradient
// (i.e. the pixel which should become one of the gradient's ends color); while for
// directions that lie in the second or fourth quadrant, that point is pixel (width, 0).
float maxPosValue =
((signbit(sinDir) != 0) == (signbit(cosDir) != 0))
? fabs(startingPos)
: fabs(startingPos+width*cosDir);
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
// Calculate the relative position of the pixel along the gradient direction
float pos = (i*cosDir + j*sinDir)/(width*cosDir + height*sinDir);
float pos = (startingPos + (i*cosDir + j*sinDir)) / maxPosValue;

float factor = pos;
factor = (factor > 1.0f)? 1.0f : factor; // Clamp to [0,1]
factor = (factor < 0.0f)? 0.0f : factor; // Clamp to [0,1]
factor = (factor > 1.0f)? 1.0f : factor; // Clamp to [-1,1]
factor = (factor < -1.0f)? -1.0f : factor; // Clamp to [-1,1]
factor = factor / 2 + 0.5f;

// Generate the color for this pixel
pixels[j*width + i].r = (int)((float)end.r*factor + (float)start.r*(1.0f - factor));
Expand Down