25 #include "../include/tscope5/graphics.h"
26 #include "../include/tscope5/graphics_internal.h"
27 #include "../include/tscope5/system_internal.h"
28 #include "../include/tscope5/textio_internal.h"
30 #include <allegro5/allegro_color.h>
84 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_coordinate_system(%d)\n",
87 int retval = _ts5_status.graphics.coordinate_system;
89 if (coordinate_system != TS5_CARTESIAN_COORDINATES
90 && coordinate_system != TS5_DISPLAY_COORDINATES) {
92 ts5_fatal(
"%s: %s (is %d)\n",
"ts5_set_coordinate_system",
93 "coordinate_system should be 0 or 1",
97 _ts5_status.graphics.coordinate_system = coordinate_system;
99 ts5_log(TS5_LOGLEVEL_4,
"%s: %s %d (was %d)\n",
100 "ts5_set_coordinate_system",
101 "set coordinate_system for next display to",
102 _ts5_status.graphics.coordinate_system, retval);
116 ts5_log(TS5_LOGLEVEL_4,
"ts5_get_coordinate_system()\n");
118 return _ts5_status.graphics.coordinate_system;
150 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_coordinate_scale(%d)\n", coordinate_scale);
152 int retval = _ts5_status.graphics.coordinate_scale;
154 if (coordinate_scale != TS5_RELATIVE_COORDINATES
155 && coordinate_scale != TS5_ABSOLUTE_COORDINATES) {
156 ts5_fatal(
"%s: %s (is %d)\n",
"ts5_set_coordinate_scale",
157 "coordinate_scale should be 0 or 1", coordinate_scale);
160 _ts5_status.graphics.coordinate_scale = coordinate_scale;
162 ts5_log(TS5_LOGLEVEL_4,
"%s: %s %d (was %d)\n",
"ts5_set_coordinate_scale",
163 "set coordinate_scale for next display to",
164 _ts5_status.graphics.coordinate_scale, retval);
178 ts5_log(TS5_LOGLEVEL_4,
"ts5_get_coordinate_scale()\n");
180 return _ts5_status.graphics.coordinate_scale;
211 ts5_log(TS5_LOGLEVEL_6,
"ts5_relative_to_absolute_coordinate_x(%f)\n", x);
215 if (_ts5_status.graphics.coordinate_system
216 == TS5_CARTESIAN_COORDINATES) {
217 retval = (x * (_ts5_status.graphics.target_width) / 2.0);
220 retval = (x * _ts5_status.graphics.target_width);
243 ts5_log(TS5_LOGLEVEL_6,
"ts5_relative_to_absolute_coordinate_y(%f)\n", y);
247 if (_ts5_status.graphics.coordinate_system
248 == TS5_CARTESIAN_COORDINATES) {
249 retval = (y * (_ts5_status.graphics.target_height) / 2.0);
252 retval = (y * _ts5_status.graphics.target_height);
274 ts5_log(TS5_LOGLEVEL_6,
"ts5_absolute_to_relative_coordinate_x(%f)\n", x);
278 if (_ts5_status.graphics.coordinate_system
279 == TS5_CARTESIAN_COORDINATES) {
280 retval = (x / (_ts5_status.graphics.target_width) * 2.0);
283 retval = (x / _ts5_status.graphics.target_width);
305 ts5_log(TS5_LOGLEVEL_6,
"ts5_absolute_to_relative_coordinate_y(%f)\n", y);
309 if (_ts5_status.graphics.coordinate_system
310 == TS5_CARTESIAN_COORDINATES) {
311 retval = (y / (_ts5_status.graphics.target_height) * 2.0);
314 retval = (y / _ts5_status.graphics.target_height);
336 ts5_log(TS5_LOGLEVEL_6,
"ts5_cartesian_to_display_coordinate_x(%f)\n", x);
338 return (_ts5_status.graphics.target_width) / 2.0 + x;
357 ts5_log(TS5_LOGLEVEL_6,
"ts5_cartesian_to_display_coordinate_y(%f)\n", y);
359 return (_ts5_status.graphics.target_height) / 2.0 - y;
378 ts5_log(TS5_LOGLEVEL_6,
"ts5_display_to_cartesian_coordinate_x(%f)\n", x);
380 return x - (_ts5_status.graphics.target_width) / 2.0;
399 ts5_log(TS5_LOGLEVEL_6,
"ts5_display_to_cartesian_coordinate_y(%f)\n", y);
401 return (_ts5_status.graphics.target_height) / 2.0 - y;
440 ts5_log(TS5_LOGLEVEL_5,
"ts5_make_rgb_color(%f,%f,%f,%f)\n", r, g, b, a);
442 if (r < 0.0|| g < 0.0 || b < 0.0 || a < 0.0
443 || r > 1.0 || g > 1.0 || b > 1.0 || a > 1.0) {
444 ts5_fatal(
"%s: %s\n",
"ts5_make_rgb_color",
445 "color values must be between 0.0 and 1.0");
448 return al_map_rgba_f(r, g, b, a);
467 ts5_log(TS5_LOGLEVEL_5,
"ts5_make_hsl_color(%f,%f,%f,%f)\n", h, s, l, a);
469 if (h < 0.0 || s < 0.0 || l < 0.0 || a < 0.0
470 || h > 360.0 || s > 1.0 || l > 1.0 || a > 1.0) {
471 ts5_fatal(
"%s: %s\n",
"ts5_make_hsl_color",
472 "h:0.0-360.0, s:0.0-1.0, l:0.0-1.0, a:0.0-1.0");
476 al_color_hsl_to_rgb(h, s, l, &r, &g, &b);
478 return al_map_rgba_f(r, g, b, a);
497 ts5_log(TS5_LOGLEVEL_5,
"ts5_make_hsv_color(%f,%f,%f,%f)\n", h, s, v, a);
499 if (h < 0.0 || s < 0.0 || v < 0.0 || a < 0.0
500 || h > 360.0 || s > 1.0 || v > 1.0 || a > 1.0) {
501 ts5_fatal(
"%s: %s\n",
"ts5_make_hsv_color",
502 "h:0.0-360.0, s:0.0-1.0, v:0.0-1.0, a:0.0-1.0");
506 al_color_hsv_to_rgb(h, s, v, &r, &g, &b);
508 return al_map_rgba_f(r, g, b, a);
552 ts5_log(TS5_LOGLEVEL_5,
"ts5_make_named_color(%s,%f)\n", name, a);
556 if (!al_color_name_to_rgb(name, &r, &g, &b)) {
557 ts5_fatal(
"ts5_make_named_color: color name %s not found\n", name);
560 return al_map_rgba_f(r, g, b, a);
610 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_foreground_color((%f,%f,%f,%f))\n",
611 foreground_color.r, foreground_color.g, foreground_color.b,
614 TS5_COLOR oldcolor = _ts5_status.graphics.foreground_color;
615 _ts5_status.graphics.foreground_color = foreground_color;
617 unsigned char r, g, b, a;
618 al_unmap_rgba(foreground_color, &r, &g, &b, &a);
620 ts5_log(TS5_LOGLEVEL_4,
"%s: r:%f, g:%f, b:%f, alpha:%f\n",
621 "ts5_set_foreground_color", r/256, g/256, b/256, a/256);
635 ts5_log(TS5_LOGLEVEL_4,
"ts5_get_foreground_color()\n");
637 return _ts5_status.graphics.foreground_color;
657 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_background_color((%f,%f,%f,%f))\n",
658 background_color.r, background_color.g, background_color.b,
661 TS5_COLOR oldcolor = _ts5_status.graphics.background_color;
662 _ts5_status.graphics.background_color = background_color;
664 unsigned char r, g, b, a;
665 al_unmap_rgba(background_color, &r, &g, &b, &a);
666 ts5_log(TS5_LOGLEVEL_4,
"%s: r:%f, g:%f, b:%f, alpha:%f\n",
667 "ts5_set_background_color", r/256, g/256, b/256, a/256);
681 ts5_log(TS5_LOGLEVEL_4,
"ts5_get_background_color()\n");
683 return _ts5_status.graphics.background_color;
702 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_drawing_thickness(%f)\n",
705 double retval = _ts5_status.graphics.drawing_thickness;
707 if (drawing_thickness < 0.0) {
708 drawing_thickness = -1.0;
711 _ts5_status.graphics.drawing_thickness = drawing_thickness;
713 ts5_log(TS5_LOGLEVEL_4,
"%s: %s %f (was %f)\n",
714 "ts5_set_drawing_thickness",
"set drawing_thickness to",
715 _ts5_status.graphics.drawing_thickness, retval);
729 ts5_log(TS5_LOGLEVEL_4,
"ts5_get_drawing_thickness()\n");
731 return _ts5_status.graphics.drawing_thickness;
749 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_fill_mode(%d)\n", fill_mode);
751 int retval = _ts5_status.graphics.fill_mode;
753 if (fill_mode != TS5_FILL_ON && fill_mode != TS5_FILL_OFF) {
754 ts5_fatal(
"%s: %s %s\n",
"ts5_set_fill_mode",
"fill_mode should be",
755 "TS5_FILL_OFF (off) or TS5_FILL_ON (on)");
758 _ts5_status.graphics.fill_mode = fill_mode;
760 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_fill_mode: set mode to %d (was %d)\n",
761 _ts5_status.graphics.fill_mode, retval);
775 ts5_log(TS5_LOGLEVEL_4,
"ts5_get_fill_mode()\n");
777 return _ts5_status.graphics.fill_mode;
793 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_font_type(%d)\n", font_type);
795 int retval = _ts5_status.graphics.font_type;
797 if (font_type != TS5_COURIER
798 && font_type != TS5_ARIAL
799 && font_type != TS5_TIMES) {
801 ts5_fatal(
"%s: %s %s\n",
"ts5_set_font_type",
"font_type should be",
802 "TS5_COURIER, TS5_ARIAL or TS5_TIMES");
805 _ts5_status.graphics.font_type = font_type;
807 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_font_type: set font type to %d (was %d)\n",
808 _ts5_status.graphics.font_type, retval);
810 _ts5_status.graphics.font_index =
811 _ts5_status.graphics.font_type * TS5_FONTSTYLES * TS5_FONTSIZES +
812 _ts5_status.graphics.font_style * TS5_FONTSIZES +
813 _ts5_status.graphics.font_size - 8;
827 ts5_log(TS5_LOGLEVEL_4,
"ts5_get_font_type()\n");
829 return _ts5_status.graphics.font_type;
846 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_font_style(%d)\n", font_style);
848 int retval = _ts5_status.graphics.font_style;
850 if (font_style != TS5_REGULAR
851 && font_style != TS5_BOLD
852 && font_style != TS5_ITALIC
853 && font_style != TS5_BOLD_ITALIC) {
855 ts5_fatal(
"%s: %s %s\n",
"ts5_set_font_style",
"font_style should be",
856 "TS5_REGULAR, TS5_BOLD, TS5_ITALIC or TS5_BOLD_ITALIC");
859 _ts5_status.graphics.font_style = font_style;
861 ts5_log(TS5_LOGLEVEL_4,
"%s: %s %d (was %d)\n",
862 "ts5_set_font_style",
"set font style to",
863 _ts5_status.graphics.font_style, retval);
865 _ts5_status.graphics.font_index =
866 _ts5_status.graphics.font_type * TS5_FONTSTYLES * TS5_FONTSIZES +
867 _ts5_status.graphics.font_style * TS5_FONTSIZES +
868 _ts5_status.graphics.font_size - 8;
882 ts5_log(TS5_LOGLEVEL_4,
"ts5_get_font_style()\n");
884 return _ts5_status.graphics.font_style;
900 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_font_size(%d)\n", font_size);
902 int retval = _ts5_status.graphics.font_size;
904 if (font_size < 8 || font_size > 107) {
905 ts5_fatal(
"ts5_set_font_size: font_size should be between 8 and 107\n");
908 _ts5_status.graphics.font_size = font_size;
910 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_font_size: set font size to %d (was %d)\n",
911 _ts5_status.graphics.font_size, retval);
913 _ts5_status.graphics.font_index =
914 _ts5_status.graphics.font_type * TS5_FONTSTYLES * TS5_FONTSIZES +
915 _ts5_status.graphics.font_style * TS5_FONTSIZES +
916 _ts5_status.graphics.font_size - 8;
931 ts5_log(TS5_LOGLEVEL_4,
"ts5_get_font_size()\n");
933 int size = _ts5_status.graphics.font_size;
951 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_font_index(%d)\n", font_index);
953 int retval = _ts5_status.graphics.font_index;
955 if (font_index < 0 || font_index >= TS5_NFONTS) {
956 ts5_fatal(
"%s: %s",
"ts5_set_font_index",
957 "font_index should be between 0 and TS5_NFONTS-1\n");
960 _ts5_status.graphics.font_index = font_index;
963 _ts5_status.graphics.font_size =
964 (font_index % TS5_FONTSIZES) + 8;
966 _ts5_status.graphics.font_style =
967 (font_index / TS5_FONTSIZES) % TS5_FONTSTYLES;
969 _ts5_status.graphics.font_type =
970 font_index / (TS5_FONTSIZES * TS5_FONTSTYLES);
972 ts5_log(TS5_LOGLEVEL_4,
"%s: %s %d (was %d)\n",
973 "ts5_set_font_index",
"set font index to",
974 _ts5_status.graphics.font_index, retval);
988 ts5_log(TS5_LOGLEVEL_4,
"ts5_get_font_index()\n");
990 return _ts5_status.graphics.font_index;
1007 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_text_alignment(%d)\n", text_alignment);
1009 int retval = _ts5_status.graphics.text_alignment;
1011 if (text_alignment != TS5_ALIGN_LEFT
1012 && text_alignment != TS5_ALIGN_CENTER
1013 && text_alignment != TS5_ALIGN_RIGHT) {
1015 ts5_fatal(
"%s: %s %s\n",
"ts5_set_text_alignment",
1016 "text_alignment should be",
1017 "TS5_ALIGN_LEFT, TS5_ALIGN_CENTER or TS5_ALIGN_RIGHT");
1020 _ts5_status.graphics.text_alignment = text_alignment;
1022 ts5_log(TS5_LOGLEVEL_4,
"%s: %s %d (was %d)\n",
1023 "ts5_set_text_alignment",
"set alignment to",
1024 _ts5_status.graphics.text_alignment, retval);
1038 ts5_log(TS5_LOGLEVEL_4,
"ts5_get_text_alignment()\n");
1040 return _ts5_status.graphics.text_alignment;
double ts5_display_to_cartesian_coordinate_y(const double y)
Convert a vertical display coordinate into a vertical Cartisian coordinate.
TS5_COLOR ts5_make_named_color(const char *name, const double a)
Return a color that corresponds to name.
int ts5_get_coordinate_scale()
Get the coordinate scale.
int ts5_get_coordinate_system()
Get the coordinate system.
TS5_COLOR ts5_make_hsl_color(const double h, const double s, const double l, const double a)
Generate a color specification based on an HSL triplet plus an alpha channel.
void ts5_check_textio(char *calling_function)
Do some checks at the start of each textio function.
int ts5_set_font_size(int font_size)
Set the font size.
double ts5_display_to_cartesian_coordinate_x(const double x)
Convert a horizontal display coordinate into a horizontal Cartisian coordinate.
int ts5_get_fill_mode()
Get the fill mode.
int ts5_set_fill_mode(const int fill_mode)
Set the fill mode.
void ts5_check_graphics(char *calling_function)
Do some checks at the start of each graphics function.
int ts5_get_text_alignment()
Get the alignment of text.
TS5_COLOR ts5_set_foreground_color(const TS5_COLOR foreground_color)
Set the foreground color.
int ts5_set_coordinate_scale(const int coordinate_scale)
Set the coordinate scale.
TS5_COLOR ts5_get_foreground_color()
Get the foreground color.
double ts5_get_drawing_thickness()
Get the drawing thickness.
double ts5_relative_to_absolute_coordinate_x(const double x)
Convert a relative horizontal coordinate into an absolute horizontal coordinate.
int ts5_get_font_style()
Get the font style.
double ts5_set_drawing_thickness(double drawing_thickness)
Set the drawing thickness.
int ts5_set_font_style(const int font_style)
Set the font style.
TS5_COLOR ts5_set_background_color(const TS5_COLOR background_color)
Set the background color.
int ts5_set_text_alignment(const int text_alignment)
Set the alignment of text.
int ts5_get_font_type()
Get the font type.
void ts5_log(const unsigned int level, const char *format,...)
Send info to a logging window.
double ts5_cartesian_to_display_coordinate_x(const double x)
Convert a horizontal Cartesian coordinate into a horizontal display coordinate.
int ts5_set_font_index(const int font_index)
Set the font index.
double ts5_cartesian_to_display_coordinate_y(const double y)
Convert a vertical Cartesian coordinate into a vertical display coordinate.
int ts5_set_font_type(const int font_type)
Set the font type.
TS5_COLOR ts5_make_hsv_color(const double h, const double s, const double v, const double a)
Generate a color specification based on an HSV triplet plus an alpha channel.
double ts5_absolute_to_relative_coordinate_y(const double y)
Convert an absolute vertical coordinate into a relative vertical coordinate.
int ts5_get_font_index()
Get the font index.
TS5_COLOR ts5_make_rgb_color(const double r, const double g, const double b, const double a)
Generate a color specification based on an RGB triplet plus an alpha channel.
double ts5_absolute_to_relative_coordinate_x(const double x)
Convert an absolute horizontal coordinate into a relative horizontal coordinate.
void ts5_fatal(const char *format,...)
Exit safely with an error message.
double ts5_relative_to_absolute_coordinate_y(const double y)
Convert a relative vertical coordinate into an absolute vertical coordinate.
int ts5_get_font_size()
Get the font size.
int ts5_set_coordinate_system(const int coordinate_system)
Set the coordinate system.
TS5_COLOR ts5_get_background_color()
Get the background color.