Download code

1/* The authors of this work have released all rights to it and placed it
2 in the public domain under the Creative Commons CC0 1.0 waiver
3 (http://creativecommons.org/publicdomain/zero/1.0/).
4 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1213 Retrieved from: http://en.literateprograms.org/Linear_gradient_(C)?oldid=19179
14 */151617#ifndef LINEAR_GRADIENT_H18#define LINEAR_GRADIENT_H1920typedefstructImage_t*Image;21typedefuint8_t*DATA8;2223structImage_t24{25intwidth, height, bpp;26intstride;27DATA8bitmap;28};2930typedefstructColorStop_t*ColorStop;3132structColorStop_t33{34uint8_trgba[4];35intdist;36};3738intLinearGradient(Imageimg, intx1, inty1, intx2, inty2, ColorStopcs, intcount);39#endif40

1# The authors of this work have released all rights to it and placed it 2# in the public domain under the Creative Commons CC0 1.0 waiver
3 # (http://creativecommons.org/publicdomain/zero/1.0/). 4#
5 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
6 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 7# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
8 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 9# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.12#
13 # Retrieved from: http://en.literateprograms.org/Linear_gradient_(C)?oldid=191791415 gcc gradient_v1.c -o gradient_v1 `sdl-config --cflags --libs`
16 ./gradient_v1

1# The authors of this work have released all rights to it and placed it 2# in the public domain under the Creative Commons CC0 1.0 waiver
3 # (http://creativecommons.org/publicdomain/zero/1.0/). 4#
5 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
6 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 7# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
8 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 9# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.12#
13 # Retrieved from: http://en.literateprograms.org/Linear_gradient_(C)?oldid=191791415 gcc gradient_with_GradState.c -o gradient_with_GradState `sdl-config --cflags --libs`
16 ./gradient_with_GradState

1/* The authors of this work have released all rights to it and placed it
2 in the public domain under the Creative Commons CC0 1.0 waiver
3 (http://creativecommons.org/publicdomain/zero/1.0/).
4 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1213 Retrieved from: http://en.literateprograms.org/Linear_gradient_(C)?oldid=19179
14 */1516#include <SDL/SDL.h>17#include <math.h>18#include "gradient_general.h"1920#define WINW 50021#define WINH 40022#define NBCOL 223staticstructColorStop_tcolors[NBCOL]={{{255, 0, 0}}, {{20, 20, 255}}};24SDL_Surface*screen;25intangle;2627staticUint32push_ticks(Uint32interval, void*param)28{29SDL_Eventevent={.type=SDL_USEREVENT};30SDL_PushEvent(&event);31returninterval;32}3334staticvoidreset(Imageimg)35{36angle++;3738intx=cos(angle*M_PI / 180)*220;39inty=sin(angle*M_PI / 180)*220;40memset(img->bitmap, 0, img->stride*img->height);41LinearGradient(img, WINW/2-x, WINH/2-y, WINW/2+x, WINH/2+y, colors, NBCOL);42SDL_UpdateRect(screen, 0, 0, screen->w, screen->h);43}4445intmain(intargc, char*argv[])46{47if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER)<0)48return1;4950atexit(SDL_Quit);5152screen=SDL_SetVideoMode(WINW, WINH, 32, SDL_HWSURFACE|SDL_ANYFORMAT);53if(!screen)return1;5455structImage_timg={56 .bitmap=screen->pixels, .stride=screen->pitch, .width=screen->w, .height=screen->h,
57 .bpp=screen->format->BytesPerPixel58};5960inti;6162/* We need to convert colors into native format */63for(i=0;i<NBCOL;i++)64{65DATA8rgba=colors[i].rgba;66uint32_tcol=SDL_MapRGBA(screen->format, rgba[0], rgba[1], rgba[2], rgba[3]);67SETU32(colors[i].rgba, &col);68}6970SDL_AddTimer(10, push_ticks, NULL);7172while(1)73{74SDL_Eventevent;75while(SDL_WaitEvent(&event))76{77switch(event.type){78caseSDL_USEREVENT:reset(&img);break;79caseSDL_QUIT:return0;80caseSDL_KEYDOWN:81if(event.key.keysym.sym==SDLK_ESCAPE)return0;82}83}84}85return0;86}8788#ifdef WIN3289#include <windows.h>90intWINAPIWinMain(HINSTANCEinstance,
91HINSTANCEprevinst,
92LPSTRargs,
93intwndState)94{95returnmain(0, NULL);96}97#endif98

1# The authors of this work have released all rights to it and placed it 2# in the public domain under the Creative Commons CC0 1.0 waiver
3 # (http://creativecommons.org/publicdomain/zero/1.0/). 4#
5 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
6 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 7# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
8 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 9# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.12#
13 # Retrieved from: http://en.literateprograms.org/Linear_gradient_(C)?oldid=191791415 gcc gradtest.c gradient_general.c -o gradient_general `sdl-config --cflags --libs`
16 ./gradient_general
17

1/* The authors of this work have released all rights to it and placed it
2 in the public domain under the Creative Commons CC0 1.0 waiver
3 (http://creativecommons.org/publicdomain/zero/1.0/).
4 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1213 Retrieved from: http://en.literateprograms.org/Linear_gradient_(C)?oldid=19179
14 */1516#ifndef LINEAR_GRADIENT_H17#define LINEAR_GRADIENT_H1819typedefstructImage_t*Image;20typedefuint8_t*DATA8;2122structImage_t23{24intwidth, height, bpp;25intstride;26DATA8bitmap;27};2829typedefstructColorStop_t*ColorStop;3031structColorStop_t32{33uint8_trgba[4];34intdist;35};3637typedefstructIter_tIter;3839structIter_t40{41intx, y, xe, ye;42intdx, dy, err, sx, sy, oldy;43};444546typedefstructGradState_tGradState;4748structGradState_t49{50Iterr, g, b, a, pos;51intstep, dir, err;52ColorStopc;53};5455intLinearGradient(Imageimg, intx1, inty1, intx2, inty2, ColorStopcs, intcount);5657#ifndef SETU3258#define SETU32(dst, src) * (uint32_t *) (dst) = * (uint32_t *) (src)5960#endif61#ifndef swap62#define swap(a, b) do { int temp = a; a = b; b = temp; } while(0)63#endif6465#endif