Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: Testing 18.04
Views: 1010
1
// This definition are needed by the nearbyint function
2
// This definition must appear before #include<stdio.h> and #include<math.h>
3
// see the manpages documentation of nearbyint for more information
4
#define _ISOC99_SOURCE
5
#include <MLV/MLV_all.h>
6
#include <math.h>
7
#include <stdio.h>
8
typedef struct _Color {
9
int red;
10
int green;
11
int blue;
12
} Color;
13
typedef struct _Point {
14
int x;
15
int y;
16
} Point;
17
typedef struct _Triangle {
18
Point R;
19
Point G;
20
Point B;
21
} Triangle;
22
typedef struct _Graphics {
23
int width;
24
int height;
25
int width_box;
26
int height_box;
27
int height_bar;
28
Triangle triangle;
29
} Graphics;
30
void saturate_color(const Color* color, Color* result) {
31
int max = 1;
32
if ((color->red != 0.0) || (color->green != 0.0) || (color->blue != 0.0)) {
33
max = color->red;
34
if (max < color->blue) max = color->blue;
35
if (max < color->green) max = color->green;
36
}
37
result->red = (255 * color->red) / max;
38
result->green = (255 * color->green) / max;
39
result->blue = (255 * color->blue) / max;
40
}
41
typedef enum { INSIDE, OUTSIDE } Position_in_the_triangle;
42
Position_in_the_triangle get_color_of_triangle(const Point* cursor,
43
const Triangle* triangle,
44
Color* result) {
45
double rx, ry, gx, gy, bx, by;
46
double determinant;
47
double a, b, c;
48
Position_in_the_triangle position;
49
rx = triangle->R.x - cursor->x;
50
gx = triangle->G.x - cursor->x;
51
bx = triangle->B.x - cursor->x;
52
ry = triangle->R.y - cursor->y;
53
gy = triangle->G.y - cursor->y;
54
by = triangle->B.y - cursor->y;
55
determinant = -(by - gy) * rx + (bx - gx) * ry - bx * gy + by * gx;
56
a = (-bx * gy + by * gx) / determinant;
57
b = (bx * ry - by * rx) / determinant;
58
c = (-gx * ry + gy * rx) / determinant;
59
if ((a < 0.0) || (b < 0.0) || (c < 0.0)) {
60
a = 1 / 3.0;
61
b = 1 / 3.0;
62
c = 1 / 3.0;
63
position = OUTSIDE;
64
} else {
65
position = INSIDE;
66
}
67
int nuance = 255;
68
result->red = nearbyint(nuance * a);
69
result->green = nearbyint(nuance * b);
70
result->blue = nearbyint(nuance * c);
71
return position;
72
}
73
void get_color_of_bar(const Point* cursor, const Graphics* graphics,
74
const Color* bar_color, Color* result) {
75
Color satured_color;
76
saturate_color(bar_color, &satured_color);
77
double nuance = (cursor->x) / (double)graphics->width;
78
result->red = nearbyint(nuance * satured_color.red);
79
result->green = nearbyint(nuance * satured_color.green);
80
result->blue = nearbyint(nuance * satured_color.blue);
81
}
82
typedef enum { TRIANGLE, BAR } Click_position;
83
Click_position get_color(const Point* cursor, const Graphics* graphics,
84
const Color* bar_color, Color* result) {
85
if (cursor->y >= graphics->height - graphics->height_bar) {
86
get_color_of_bar(cursor, graphics, bar_color, result);
87
return BAR;
88
} else {
89
get_color_of_triangle(cursor, &(graphics->triangle), result);
90
return TRIANGLE;
91
}
92
}
93
void draw_text(const Color* color, const Graphics* graphics,
94
int y_translation) {
95
int text_width, text_height;
96
MLV_get_size_of_text("R:%d, G:%d, B:%d, A:%d ", &text_width, &text_height,
97
color->red, color->green, color->blue, MLV_ALPHA_OPAQUE);
98
99
MLV_draw_text(graphics->width - graphics->width_box - text_width,
100
(graphics->height_box / 2) - (text_height / 2) + y_translation,
101
"R:%d, G:%d, B:%d, A:%d ", MLV_COLOR_RED, color->red,
102
color->green, color->blue, MLV_ALPHA_OPAQUE);
103
}
104
void draw_foreground(const Point* cursor, const Graphics* graphics,
105
const Color* bar_color) {
106
MLV_load_screen();
107
Color cursor_color;
108
get_color(cursor, graphics, bar_color, &cursor_color);
109
MLV_draw_filled_rectangle(graphics->width - graphics->width_box, 0,
110
graphics->width_box, graphics->height_box,
111
MLV_rgba(cursor_color.red, cursor_color.green,
112
cursor_color.blue, MLV_ALPHA_OPAQUE));
113
draw_text(&cursor_color, graphics, 0);
114
MLV_actualise_window();
115
}
116
void draw_background(const Graphics* graphics, const Color* bar_color,
117
const Color* selection_color) {
118
int width = graphics->width;
119
int height = graphics->height;
120
int height_bar = graphics->height_bar;
121
Point point;
122
Color color;
123
MLV_clear_window(MLV_COLOR_BLACK);
124
for (point.x = 0; point.x < width; point.x++) {
125
for (point.y = 0; point.y < height; point.y++) {
126
if (get_color_of_triangle(&point, &(graphics->triangle), &color) ==
127
INSIDE) {
128
MLV_draw_point(
129
point.x, point.y,
130
MLV_rgba(color.red, color.green, color.blue, MLV_ALPHA_OPAQUE));
131
}
132
}
133
}
134
int i;
135
136
Color bar_color_satured;
137
saturate_color(bar_color, &bar_color_satured);
138
139
for (i = 0; i < width; i++) {
140
MLV_Color color =
141
MLV_rgba((bar_color_satured.red * i) / width,
142
(bar_color_satured.green * i) / width,
143
(bar_color_satured.blue * i) / width, MLV_ALPHA_OPAQUE);
144
MLV_draw_line(i, height - height_bar, i, height, color);
145
}
146
MLV_draw_filled_rectangle(
147
graphics->width - graphics->width_box, graphics->height_box,
148
graphics->width_box, graphics->height_box,
149
MLV_rgba(selection_color->red, selection_color->green,
150
selection_color->blue, MLV_ALPHA_OPAQUE));
151
draw_text(selection_color, graphics, graphics->height_box);
152
MLV_save_screen();
153
}
154
void set_triangle(Graphics* graphics, int posx, int posy, int size) {
155
int height = size * sqrt(3) / 2.0;
156
graphics->triangle.R.x = posx;
157
graphics->triangle.R.y = posy + height;
158
graphics->triangle.G.x = posx + size;
159
graphics->triangle.G.y = posy + height;
160
graphics->triangle.B.x = posx + size / 2;
161
graphics->triangle.B.y = posy;
162
}
163
int main(int argc, char* argv[]) {
164
Graphics graphics;
165
graphics.width = 1024;
166
graphics.height = 960;
167
graphics.width_box = 120;
168
graphics.height_box = 80;
169
graphics.height_bar = 40;
170
set_triangle(&graphics, 50, 100, 300);
171
Point cursor;
172
cursor.x = 0;
173
cursor.y = 0;
174
MLV_Button_state state;
175
MLV_Keyboard_button key;
176
Color selection_color;
177
get_color_of_triangle(&cursor, &(graphics.triangle), &selection_color);
178
Color bar_color = selection_color;
179
MLV_Event event = MLV_NONE;
180
MLV_create_window("medium - 6 - colors", "colors", graphics.width,
181
graphics.height);
182
draw_background(&graphics, &bar_color, &selection_color);
183
draw_foreground(&cursor, &graphics, &bar_color);
184
int continue_to_run = 0;
185
while (!continue_to_run) {
186
while ((event = MLV_get_event(&key, NULL, NULL, NULL, NULL, &(cursor.x),
187
&(cursor.y), NULL, &state)) != MLV_NONE) {
188
switch (event) {
189
case MLV_MOUSE_MOTION:
190
break;
191
case MLV_MOUSE_BUTTON:
192
if (state == MLV_PRESSED) {
193
if (get_color(&cursor, &graphics, &bar_color, &selection_color) ==
194
TRIANGLE) {
195
bar_color = selection_color;
196
}
197
printf("MLV_rgba( %d , %d , %d, MLV_ALPHA_OPAQUE )\n",
198
selection_color.red, selection_color.green,
199
selection_color.blue);
200
draw_background(&graphics, &bar_color, &selection_color);
201
draw_foreground(&cursor, &graphics, &bar_color);
202
};
203
break;
204
case MLV_KEY:
205
if (key == MLV_KEYBOARD_ESCAPE) {
206
continue_to_run = 1;
207
}
208
break;
209
default:;
210
}
211
}
212
draw_foreground(&cursor, &graphics, &bar_color);
213
MLV_delay_according_to_frame_rate();
214
}
215
MLV_free_window();
216
return 0;
217
}
218
219