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