Debian Codegraph @

sys/Graphics_colour.cpp

raw symbols SCIP
1 /* Graphics_colour.cpp
2 *
3 * Copyright (C) 1992-2005,2007-2020,2022 Paul Boersma, 2013 Tom Naughton
4 *
5 * This code is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 *
10 * This code is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this work. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "GraphicsP.h"
20
21 #if quartz
22 #include "macport_on.h"
23 #endif
24
25 #define wdx(x) ((x) * my scaleX + my deltaX)
26 #define wdy(y) ((y) * my scaleY + my deltaY)
27
28 void _Graphics_setColour (Graphics graphics, MelderColour colour) {
29 if (graphics -> screen) {
30 GraphicsScreen me = static_cast <GraphicsScreen> (graphics);
31 #if cairo
32 if (! my d_cairoGraphicsContext)
33 return;
34 cairo_set_source_rgb (my d_cairoGraphicsContext, colour. red, colour. green, colour. blue);
35 #elif gdi
36 my d_winForegroundColour = RGB (colour. red * 255, colour. green * 255, colour. blue * 255);
37 SelectPen (my d_gdiGraphicsContext, GetStockPen (BLACK_PEN));
38 DeleteObject (my d_winPen);
39 my d_winPen = CreatePen (PS_SOLID, 0, my d_winForegroundColour);
40 SelectBrush (my d_gdiGraphicsContext, GetStockBrush (NULL_BRUSH));
41 DeleteObject (my d_winBrush);
42 my d_winBrush = CreateSolidBrush (my d_winForegroundColour);
43 #elif quartz
44 // postpone till drawing
45 #endif
46 } else if (graphics -> postScript) {
47 GraphicsPostscript me = static_cast <GraphicsPostscript> (graphics);
48 my d_printf (my d_file, "%.6g %.6g %.6g setrgbcolor\n", colour. red, colour. green, colour. blue);
49 }
50 }
51
52 void Graphics_setColour (Graphics me, MelderColour colour) {
53 my colour = colour;
54 _Graphics_setColour (me, colour);
55 if (my recording) { op (SET_RGB_COLOUR, 3); put (colour. red); put (colour. green); put (colour. blue); }
56 }
57
58 void Graphics_setColourScale (Graphics me, enum kGraphics_colourScale colourScale) {
59 my colourScale = colourScale;
60 if (my recording) { op (SET_COLOUR_SCALE, 1); put (colourScale); }
61 }
62
63 void _Graphics_setGrey (Graphics graphics, double fgrey) {
64 Melder_clip (0.0, & fgrey, 1.0);
65 if (graphics -> screen) {
66 GraphicsScreen me = static_cast <GraphicsScreen> (graphics);
67 #if cairo
68 if (! my d_cairoGraphicsContext)
69 return;
70 cairo_set_source_rgb (my d_cairoGraphicsContext, fgrey, fgrey, fgrey);
71 #elif gdi
72 int lightness = fgrey * 255;
73 my d_winForegroundColour = RGB (lightness, lightness, lightness);
74 SelectPen (my d_gdiGraphicsContext, GetStockPen (BLACK_PEN));
75 DeleteObject (my d_winPen);
76 my d_winPen = CreatePen (PS_SOLID, 0, my d_winForegroundColour);
77 SelectBrush (my d_gdiGraphicsContext, GetStockBrush (NULL_BRUSH));
78 DeleteObject (my d_winBrush);
79 my d_winBrush = CreateSolidBrush (my d_winForegroundColour);
80 #elif quartz
81 #endif
82 } else if (graphics -> postScript) {
83 GraphicsPostscript me = static_cast <GraphicsPostscript> (graphics);
84 my d_printf (my d_file, "%.6g setgray\n", fgrey);
85 }
86 }
87
88 void Graphics_setGrey (Graphics me, double grey) {
89 my colour. red = my colour. green = my colour. blue = grey;
90 _Graphics_setGrey (me, grey);
91 if (my recording) { op (SET_GREY, 1); put (grey); }
92 }
93
94 static void highlight (Graphics graphics, integer x1DC, integer x2DC, integer y1DC, integer y2DC) {
95 if (graphics -> screen) {
96 GraphicsScreen me = static_cast <GraphicsScreen> (graphics);
97 #if cairo
98 if (! my d_cairoGraphicsContext)
99 return;
100 const int width = x2DC - x1DC, height = y1DC - y2DC;
101 if (width <= 0 || height <= 0)
102 return;
103 if (! my d_cairoGraphicsContext)
104 return;
105 cairo_set_source_rgba (my d_cairoGraphicsContext, 1.0, 0.7, 0.7, 0.5); // using transparency
106 cairo_rectangle (my d_cairoGraphicsContext, x1DC, y2DC, width, height);
107 cairo_fill (my d_cairoGraphicsContext);
108 #elif gdi
109 static HBRUSH highlightBrush;
110 RECT rect;
111 rect. left = x1DC, rect. right = x2DC, rect. top = y2DC, rect. bottom = y1DC;
112 if (! highlightBrush)
113 highlightBrush = CreateSolidBrush (RGB (255, 210, 210));
114 SelectPen (my d_gdiGraphicsContext, GetStockPen (NULL_PEN));
115 SelectBrush (my d_gdiGraphicsContext, highlightBrush);
116 SetROP2 (my d_gdiGraphicsContext, R2_MASKPEN); // using a built-in pixel-combination formula; both R2_NOTXORPEN and R2_MASKPEN make some sense
117 Rectangle (my d_gdiGraphicsContext, x1DC, y2DC, x2DC + 1, y1DC + 1);
118 SetROP2 (my d_gdiGraphicsContext, R2_COPYPEN);
119 SelectPen (my d_gdiGraphicsContext, GetStockPen (BLACK_PEN));
120 SelectBrush (my d_gdiGraphicsContext, GetStockBrush (NULL_BRUSH)); // superfluous?
121 #elif quartz
122 const int width = x2DC - x1DC, height = y1DC - y2DC;
123 if (width <= 0 || height <= 0)
124 return;
125 GuiCocoaDrawingArea *drawingArea = (GuiCocoaDrawingArea *) my d_drawingArea -> d_widget;
126 if (! drawingArea)
127 return;
128 const NSRect rect = NSMakeRect (x1DC, y2DC, width, height);
129 CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
130 if (context) {
131 CGContextSaveGState (context);
132 //CGContextSetShouldAntialias (context, false);
133 if ((true)) {
134 CGContextSetBlendMode (context, kCGBlendModeDarken); // using a built-in pixel-combination formula
135 CGContextSetRGBFillColor (context, 1.0, 0.83, 0.83, 1.0);
136 } else {
137 CGContextSetRGBFillColor (context, 1.0, 0.5, 0.5, 0.3); // using transparency
138 }
139 CGContextFillRect (context, rect);
140 CGContextRestoreGState (context);
141 }
142 #endif
143 }
144 }
145
146 void Graphics_highlight (Graphics me, double x1WC, double x2WC, double y1WC, double y2WC) {
147 if (my recording) {
148 op (HIGHLIGHT, 4); put (x1WC); put (x2WC); put (y1WC); put (y2WC);
149 } else
150 highlight (me, wdx (x1WC), wdx (x2WC), wdy (y1WC), wdy (y2WC));
151 }
152
153 static void highlight2 (Graphics graphics, integer x1DC, integer x2DC, integer y1DC, integer y2DC,
154 integer x1DC_inner, integer x2DC_inner, integer y1DC_inner, integer y2DC_inner)
155 {
156 if (graphics -> screen) {
157 GraphicsScreen me = static_cast <GraphicsScreen> (graphics);
158 #if cairo
159 if (! my d_cairoGraphicsContext)
160 return;
161 const int width = x2DC - x1DC, height = y1DC - y2DC;
162 if (width <= 0 || height <= 0)
163 return;
164 cairo_set_source_rgba (my d_cairoGraphicsContext, 1.0, 0.7, 0.7, 0.5);
165 cairo_rectangle (my d_cairoGraphicsContext, x1DC, y2DC, x2DC - x1DC, y2DC_inner - y2DC); // upper
166 cairo_rectangle (my d_cairoGraphicsContext, x1DC, y2DC_inner, x1DC_inner - x1DC, y1DC_inner - y2DC_inner); // left part
167 cairo_rectangle (my d_cairoGraphicsContext, x2DC_inner, y2DC_inner, x2DC - x2DC_inner, y1DC_inner - y2DC_inner); // right part
168 cairo_rectangle (my d_cairoGraphicsContext, x1DC, y1DC_inner, x2DC - x1DC, y1DC - y1DC_inner); // lower
169 cairo_fill (my d_cairoGraphicsContext);
170 #elif gdi
171 static HBRUSH highlightBrush;
172 if (! highlightBrush)
173 highlightBrush = CreateSolidBrush (RGB (255, 210, 210));
174 SelectPen (my d_gdiGraphicsContext, GetStockPen (NULL_PEN));
175 SelectBrush (my d_gdiGraphicsContext, highlightBrush);
176 SetROP2 (my d_gdiGraphicsContext, R2_MASKPEN);
177 Rectangle (my d_gdiGraphicsContext, x1DC, y2DC, x2DC + 1, y2DC_inner + 1);
178 Rectangle (my d_gdiGraphicsContext, x1DC, y2DC_inner, x1DC_inner + 1, y1DC_inner + 1);
179 Rectangle (my d_gdiGraphicsContext, x2DC_inner, y2DC_inner, x2DC + 1, y1DC_inner + 1);
180 Rectangle (my d_gdiGraphicsContext, x1DC, y1DC_inner, x2DC + 1, y1DC + 1);
181 SetROP2 (my d_gdiGraphicsContext, R2_COPYPEN);
182 SelectPen (my d_gdiGraphicsContext, GetStockPen (BLACK_PEN));
183 SelectBrush (my d_gdiGraphicsContext, GetStockBrush (NULL_BRUSH)); // superfluous?
184 #elif quartz
185 GuiCocoaDrawingArea *drawingArea = (GuiCocoaDrawingArea *) my d_drawingArea -> d_widget;
186 if (! drawingArea)
187 return;
188 my d_macGraphicsContext = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
189 CGContextSaveGState (my d_macGraphicsContext);
190 NSRect upperRect = NSMakeRect (x1DC, y2DC, x2DC - x1DC, y2DC_inner - y2DC);
191 NSRect leftRect = NSMakeRect (x1DC, y2DC_inner, x1DC_inner - x1DC, y1DC_inner - y2DC_inner);
192 NSRect rightRect = NSMakeRect (x2DC_inner, y2DC_inner, x2DC - x2DC_inner, y1DC_inner - y2DC_inner);
193 NSRect lowerRect = NSMakeRect (x1DC, y1DC_inner, x2DC - x1DC, y1DC - y1DC_inner);
194 #if 0
195 NSColor *colour = [[NSColor selectedTextBackgroundColor] colorUsingColorSpaceName: NSCalibratedRGBColorSpace];
196 const double red = 0.5 + 0.5 * colour.redComponent;
197 const double green = 0.5 + 0.5 * colour.greenComponent;
198 const double blue = 0.5 + 0.5 * colour.blueComponent;
199 #endif
200 CGContextSetBlendMode (my d_macGraphicsContext, kCGBlendModeDarken);
201 CGContextSetRGBFillColor (my d_macGraphicsContext, 1.0, 0.83, 0.83, 1.0);
202 CGContextFillRect (my d_macGraphicsContext, upperRect);
203 CGContextFillRect (my d_macGraphicsContext, leftRect);
204 CGContextFillRect (my d_macGraphicsContext, rightRect);
205 CGContextFillRect (my d_macGraphicsContext, lowerRect);
206 CGContextRestoreGState (my d_macGraphicsContext);
207 #endif
208 }
209 }
210
211 void Graphics_highlight2 (Graphics me, double x1WC, double x2WC, double y1WC, double y2WC,
212 double x1WC_inner, double x2WC_inner, double y1WC_inner, double y2WC_inner)
213 {
214 if (my recording) {
215 op (HIGHLIGHT2, 8);
216 put (x1WC); put (x2WC); put (y1WC); put (y2WC);
217 put (x1WC_inner); put (x2WC_inner); put (y1WC_inner); put (y2WC_inner);
218 } else
219 highlight2 (me, wdx (x1WC), wdx (x2WC), wdy (y1WC), wdy (y2WC), wdx (x1WC_inner), wdx (x2WC_inner), wdy (y1WC_inner), wdy (y2WC_inner));
220 }
221
222 void Graphics_xorOn (Graphics graphics, MelderColour colourOnWhiteBackground) {
223 if (graphics -> screen) {
224 GraphicsScreen me = static_cast <GraphicsScreen> (graphics);
225 if (my recording) {
226 op (XOR_ON, 3);
227 put (colourOnWhiteBackground. red);
228 put (colourOnWhiteBackground. green);
229 put (colourOnWhiteBackground. blue);
230 } else {
231 my colour. red = 1.0 - colourOnWhiteBackground. red;
232 my colour. green = 1.0 - colourOnWhiteBackground. green;
233 my colour. blue = 1.0 - colourOnWhiteBackground. blue;
234 #if cairo
235 cairo_set_operator (my d_cairoGraphicsContext, CAIRO_OPERATOR_DIFFERENCE);
236 #elif gdi
237 SetROP2 (my d_gdiGraphicsContext, R2_XORPEN);
238 #elif quartz
239 CGContextSetBlendMode (my d_macGraphicsContext, kCGBlendModeDifference);
240 #endif
241 _Graphics_setColour (me, my colour);
242 my duringXor = true;
243 }
244 }
245 }
246
247 void Graphics_xorOff (Graphics graphics) {
248 if (graphics -> screen) {
249 GraphicsScreen me = static_cast <GraphicsScreen> (graphics);
250 if (my recording) {
251 op (XOR_OFF, 0);
252 } else {
253 #if cairo
254 cairo_set_operator (my d_cairoGraphicsContext, CAIRO_OPERATOR_OVER);
255 #elif gdi
256 SetROP2 (my d_gdiGraphicsContext, R2_COPYPEN);
257 #elif quartz
258 CGContextSetBlendMode (my d_macGraphicsContext, kCGBlendModeNormal);
259 #endif
260 _Graphics_setColour (me, my colour);
261 my duringXor = false;
262 }
263 }
264 }
265
266 MelderColour Graphics_inqColour (Graphics me) {
267 return my colour;
268 }
269
270 enum kGraphics_colourScale Graphics_inqColourScale (Graphics me) {
271 return my colourScale;
272 }
273
274 /* End of file Graphics_colour.cpp */