about summary refs log tree commit diff
path: root/generator/ppmwheel.c
blob: ef5021f982c4de1a8b04472fbe9db5988b3fc5ba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* ppmwheel.c - create a color circle of a specified size
**
** This was adapted by Bryan Henderson in January 2003 from ppmcirc.c by
** Peter Kirchgessner:
**
** Copyright (C) 1995 by Peter Kirchgessner.
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation.  This software is provided "as is" without express or
** implied warranty.
*/

#include <string.h>
#include <math.h>

#include "ppm.h"

#ifndef PI
#define PI  3.14159265358979323846
#endif

#ifndef ABS
#define ABS(a) ((a) < 0 ? -(a) : (a))
#endif

static void 
hsv_rgb(double const in_h, double const in_s, double const in_v, 
        double * const r, double * const g, double * const b) {
/*----------------------------------------------------------------------------
   This is a stripped down hsv->rgb converter that works only for
   Saturation of zero.
-----------------------------------------------------------------------------*/
    double h, s, v;

    h = in_h < 0.0 ? 0.0 : in_h > 360.0 ? 360.0 : in_h;

    v = in_v < 0.0 ? 0.0 : in_v > 1.0 ? 1.0 : in_v;

    s = in_s < 0.0 ? 0.0 : in_s > 1.0 ? 1.0 : in_s;

    if (s != 0.0)
        pm_error("Internal error: non-zero saturation");

    if (h <= 60.0) {          /* from red to yellow */
        *r = 1.0;
        *g = h / 60.0;
        *b = 0.0;
    } else if ( h <= 120.0 ) {   /* from yellow to green */
        *r = 1.0 - (h - 60.0) / 60.0;
        *g = 1.0;
        *b = 0.0;
    } else if ( h <= 180.0 ) {   /* from green to cyan */
        *r = 0.0;
        *g = 1.0;
        *b = (h - 120.0) / 60.0;
    } else if ( h <= 240.0 ) {    /* from cyan to blue */
        *r = 0.0;
        *g = 1.0 - (h - 180.0) / 60.0;
        *b = 1.0;
    } else if ( h <= 300.0) {    /* from blue to magenta */
        *r = (h - 240.0) / 60.0;
        *g = 0.0;
        *b = 1.0;
    } else {                      /* from magenta to red */
        *r = 1.0;
        *g = 0.0;
        *b = 1.0 - (h - 300.0) / 60.0;
    }

    if ( v >= 0.5) {
        v = 2.0 - 2.0 * v;
        v = sqrt (v);
        *r = 1.0 + v * (*r - 1.0);
        *g = 1.0 + v * (*g - 1.0);
        *b = 1.0 + v * (*b - 1.0);
    } else {
        v *= 2.0;
        v = sqrt (sqrt ( sqrt (v)));
        *r *= v;
        *g *= v;
        *b *= v;
    }
}


int
main(int argc, char *argv[]) {
    pixel *orow;
    int rows, cols;
    pixval maxval;
    unsigned int row;
    unsigned int xcenter, ycenter, radius;
    long diameter;
    char * tailptr;

    ppm_init( &argc, argv );

    if (argc-1 != 1)
        pm_error("Program takes one argument:  diameter of color wheel");

    diameter = strtol(argv[1], &tailptr, 10);
    if (strlen(argv[1]) == 0 || *tailptr != '\0')
        pm_error("You specified an invalid diameter: '%s'", argv[1]);
    if (diameter <= 0)
        pm_error("Diameter must be positive.  You specified %ld.", diameter);
    if (diameter < 4)
        pm_error("Diameter must be at least 4.  You specified %ld", diameter);

    cols = rows = diameter;
    
    orow = ppm_allocrow(cols);

    maxval = PPM_MAXMAXVAL;
    ppm_writeppminit(stdout, cols, rows, maxval, 0);

    radius = diameter/2 - 1;

    xcenter = cols / 2;
    ycenter = rows / 2;

    for (row = 0; row < rows; ++row) {
        unsigned int col;
        for (col = 0; col < cols; ++col) {
            double const dx = (int)col - (int)xcenter;
            double const dy = (int)row - (int)ycenter;
            double const dist = sqrt(dx*dx + dy*dy);

            pixval r, g, b;

            if (dist > radius) {
                r = g = b = maxval;
            } else {
                double hue, sat, val;
                double dr, dg, db;

                hue = atan2(dx, dy) / PI * 180.0;
                if (hue < 0.0) 
                    hue = 360.0 + hue;
                sat = 0.0;
                val = dist / radius;

                hsv_rgb(hue, sat, val, &dr, &dg, &db);

                r = (pixval)(maxval * dr);
                g = (pixval)(maxval * dg);
                b = (pixval)(maxval * db);
            }
            PPM_ASSIGN (orow[col], r, g, b );
        }
        ppm_writeppmrow(stdout, orow, cols, maxval, 0);
    }
    pm_close(stdout);
    exit(0);
}