diff options
| author | Dane Madsen <[email protected]> | 2023-05-21 19:33:47 +1000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-05-21 11:33:47 +0200 |
| commit | e96dc46d38fcc73cc278ef7a1ad95ce63c6ce989 (patch) | |
| tree | dce80cf3f5d6b9cff2486da6e0f0e3ea6df02f01 /src | |
| parent | 1b4634702ca9f1e76477833618ccfea9093f03e5 (diff) | |
| download | raylib-e96dc46d38fcc73cc278ef7a1ad95ce63c6ce989.tar.gz raylib-e96dc46d38fcc73cc278ef7a1ad95ce63c6ce989.zip | |
Replaced GenImageGradientH and GenImageGradientV with GenImageLinearGradient (#3074)
* Replaced GenImageGradientH and GenImageGradientV with GenImageLinearGradient
* renamed GenImageLinearGradient to GenImageGradientLinear
Diffstat (limited to 'src')
| -rw-r--r-- | src/raylib.h | 3 | ||||
| -rw-r--r-- | src/rtextures.c | 59 |
2 files changed, 24 insertions, 38 deletions
diff --git a/src/raylib.h b/src/raylib.h index 2a5a2f63..edc81c34 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1239,8 +1239,7 @@ RLAPI bool ExportImageAsCode(Image image, const char *fileName); // Image generation functions RLAPI Image GenImageColor(int width, int height, Color color); // Generate image: plain color -RLAPI Image GenImageGradientV(int width, int height, Color top, Color bottom); // Generate image: vertical gradient -RLAPI Image GenImageGradientH(int width, int height, Color left, Color right); // Generate image: horizontal gradient +RLAPI Image GenImageGradientLinear(int width, int height, int direction, Color start, Color end); // Generate image: linear gradient RLAPI Image GenImageGradientRadial(int width, int height, float density, Color inner, Color outer); // Generate image: radial gradient RLAPI Image GenImageChecked(int width, int height, int checksX, int checksY, Color col1, Color col2); // Generate image: checked RLAPI Image GenImageWhiteNoise(int width, int height, float factor); // Generate image: white noise diff --git a/src/rtextures.c b/src/rtextures.c index 87594258..d1cebe0d 100644 --- a/src/rtextures.c +++ b/src/rtextures.c @@ -684,48 +684,35 @@ Image GenImageColor(int width, int height, Color color) } #if defined(SUPPORT_IMAGE_GENERATION) -// Generate image: vertical gradient -Image GenImageGradientV(int width, int height, Color top, Color bottom) +// Generate image: linear gradient +// The direction value specifies the direction of the gradient (in degrees) +// with 0 being vertical (from top to bottom), 90 being horizontal (from left to right). +// The gradient effectively rotates counter-clockwise by the specified amount. +Image GenImageGradientLinear(int width, int height, int direction, Color start, Color end) { Color *pixels = (Color *)RL_MALLOC(width*height*sizeof(Color)); - for (int j = 0; j < height; j++) - { - float factor = (float)j/(float)height; - for (int i = 0; i < width; i++) - { - pixels[j*width + i].r = (int)((float)bottom.r*factor + (float)top.r*(1.f - factor)); - pixels[j*width + i].g = (int)((float)bottom.g*factor + (float)top.g*(1.f - factor)); - pixels[j*width + i].b = (int)((float)bottom.b*factor + (float)top.b*(1.f - factor)); - pixels[j*width + i].a = (int)((float)bottom.a*factor + (float)top.a*(1.f - factor)); - } - } - - Image image = { - .data = pixels, - .width = width, - .height = height, - .format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8, - .mipmaps = 1 - }; - - return image; -} - -// Generate image: horizontal gradient -Image GenImageGradientH(int width, int height, Color left, Color right) -{ - Color *pixels = (Color *)RL_MALLOC(width*height*sizeof(Color)); + float radianDirection = (float)(90 - direction) / 180.f * 3.14159f; + float cosDir = cos(radianDirection); + float sinDir = sin(radianDirection); - for (int i = 0; i < width; i++) + int i, j; + for (i = 0; i < width; i++) { - float factor = (float)i/(float)width; - for (int j = 0; j < height; j++) + for (j = 0; j < height; j++) { - pixels[j*width + i].r = (int)((float)right.r*factor + (float)left.r*(1.f - factor)); - pixels[j*width + i].g = (int)((float)right.g*factor + (float)left.g*(1.f - factor)); - pixels[j*width + i].b = (int)((float)right.b*factor + (float)left.b*(1.f - factor)); - pixels[j*width + i].a = (int)((float)right.a*factor + (float)left.a*(1.f - factor)); + // Calculate the relative position of the pixel along the gradient direction + float pos = (i * cosDir + j * sinDir) / (width * cosDir + height * sinDir); + + float factor = pos; + factor = (factor > 1.f) ? 1.f : factor; // Clamp to [0,1] + factor = (factor < 0.f) ? 0.f : factor; // Clamp to [0,1] + + // Generate the color for this pixel + pixels[j * width + i].r = (int)((float)end.r*factor + (float)start.r*(1.f - factor)); + pixels[j * width + i].g = (int)((float)end.g*factor + (float)start.g*(1.f - factor)); + pixels[j * width + i].b = (int)((float)end.b*factor + (float)start.b*(1.f - factor)); + pixels[j * width + i].a = (int)((float)end.a*factor + (float)start.a*(1.f - factor)); } } |
