xref: /haiku/src/servers/app/drawing/PatternHandler.cpp (revision fef6144999c2fa611f59ee6ffe6dd7999501385c)
1 //------------------------------------------------------------------------------
2 //	Copyright (c) 2001-2005, Haiku, Inc.
3 //
4 //	Permission is hereby granted, free of charge, to any person obtaining a
5 //	copy of this software and associated documentation files (the "Software"),
6 //	to deal in the Software without restriction, including without limitation
7 //	the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 //	and/or sell copies of the Software, and to permit persons to whom the
9 //	Software is furnished to do so, subject to the following conditions:
10 //
11 //	The above copyright notice and this permission notice shall be included in
12 //	all copies or substantial portions of the Software.
13 //
14 //	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 //	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 //	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 //	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 //	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 //	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 //	DEALINGS IN THE SOFTWARE.
21 //
22 //	File Name:		PatternHandler.cpp
23 //	Author:			DarkWyrm <bpmagic@columbus.rr.com>
24 //					Stephan Aßmus <superstippi@gmx.de>
25 //	Description:	Class for easy calculation and use of patterns
26 //
27 //------------------------------------------------------------------------------
28 #include <Point.h>
29 #include "PatternHandler.h"
30 
31 const Pattern kSolidHigh(0xFFFFFFFFFFFFFFFFLL);
32 const Pattern kSolidLow((uint64)0);
33 const Pattern kMixedColors(0xAAAAAAAAAAAAAAAALL);
34 
35 const rgb_color kBlack = (rgb_color){ 0, 0, 0, 255 };
36 const rgb_color kWhite = (rgb_color){ 255, 255, 255, 255 };
37 
38 /*!
39 	\brief Void constructor
40 
41 	The pattern is set to B_SOLID_HIGH, high color is set to black, and
42 	low color is set to white.
43 */
44 PatternHandler::PatternHandler(void)
45 	: fPattern(kSolidHigh),
46 	  fHighColor(kBlack),
47 	  fLowColor(kWhite)
48 {
49 }
50 
51 /*!
52 	\brief Constructor initializes to given pattern
53 	\param pat Pattern to use.
54 
55 	This initializes to the given pattern or B_SOLID_HIGH if the pattern
56 	is NULL. High color is set to black, and low color is set to white.
57 */
58 PatternHandler::PatternHandler(const int8* pat)
59 	: fPattern(pat ? Pattern(pat) : Pattern(kSolidHigh)),
60 	  fHighColor(kBlack),
61 	  fLowColor(kWhite)
62 {
63 }
64 
65 /*!
66 	\brief Constructor initializes to given pattern
67 	\param pat Pattern to use.
68 
69 	This initializes to the given pattern or B_SOLID_HIGH if the pattern
70 	is NULL. High color is set to black, and low color is set to white.
71 */
72 PatternHandler::PatternHandler(const uint64& pat)
73 	: fPattern(pat),
74 	  fHighColor(kBlack),
75 	  fLowColor(kWhite)
76 {
77 }
78 
79 /*!
80 	\brief Constructor initializes to given pattern
81 	\param pat Pattern to use.
82 
83 	This initializes to the given Pattern.
84 	High color is set to black, and low color is set to white.
85 */
86 PatternHandler::PatternHandler(const Pattern& pat)
87 	: fPattern(pat),
88 	  fHighColor(kBlack),
89 	  fLowColor(kWhite)
90 {
91 }
92 
93 /*!
94 	\brief Constructor initializes to given PatternHandler
95 	\param other PatternHandler to copy.
96 
97 	Copy constructor.
98 */
99 PatternHandler::PatternHandler(const PatternHandler& other)
100 	: fPattern(other.fPattern),
101 	  fHighColor(other.fHighColor),
102 	  fLowColor(other.fLowColor)
103 {
104 }
105 
106 //! Destructor does nothing
107 PatternHandler::~PatternHandler(void)
108 {
109 }
110 
111 /*!
112 	\brief Sets the pattern for the handler to the one given
113 	\param pat Pattern to use.
114 
115 	This initializes to the given pattern or B_SOLID_HIGH if the pattern
116 	is NULL.
117 */
118 void PatternHandler::SetPattern(const int8* pat)
119 {
120 	if (pat)
121 		fPattern.Set(pat);
122 	else
123 		fPattern = kSolidHigh;
124 }
125 
126 /*!
127 	\brief Sets the pattern for the handler to the one given
128 	\param pat Pattern to use.
129 */
130 void PatternHandler::SetPattern(const uint64& pat)
131 {
132 	fPattern = pat;
133 }
134 
135 /*!
136 	\brief Sets the pattern for the handler to the one given
137 	\param pat Pattern to use.
138 */
139 void PatternHandler::SetPattern(const Pattern& pat)
140 {
141 	fPattern = pat;
142 }
143 
144 /*!
145 	\brief Sets the pattern for the handler to the one given
146 	\param pat R5 style pattern to use.
147 */
148 void PatternHandler::SetPattern(const pattern& pat)
149 {
150 	fPattern = pat;
151 }
152 
153 /*!
154 	\brief Set the colors for the pattern to use
155 	\param high High color for the handler
156 	\param low Low color for the handler
157 */
158 void PatternHandler::SetColors(const RGBColor& high, const RGBColor& low)
159 {
160 	fHighColor = high;
161 	fLowColor = low;
162 }
163 
164 /*!
165 	\brief Set the high color for the pattern to use
166 	\param color High color for the handler
167 */
168 void PatternHandler::SetHighColor(const RGBColor& color)
169 {
170 	fHighColor = color;
171 }
172 
173 /*!
174 	\brief Set the low color for the pattern to use
175 	\param color Low color for the handler
176 */
177 void PatternHandler::SetLowColor(const RGBColor& color)
178 {
179 	fLowColor = color;
180 }
181 
182 /*!
183 	\brief Set the colors for the pattern to use
184 	\param high High color for the handler
185 	\param low Low color for the handler
186 */
187 void PatternHandler::SetColors(const rgb_color& high, const rgb_color& low)
188 {
189 	fHighColor = high;
190 	fLowColor = low;
191 }
192 
193 /*!
194 	\brief Set the high color for the pattern to use
195 	\param color High color for the handler
196 */
197 void PatternHandler::SetHighColor(const rgb_color& color)
198 {
199 	fHighColor = color;
200 }
201 
202 /*!
203 	\brief Set the low color for the pattern to use
204 	\param color Low color for the handler
205 */
206 void PatternHandler::SetLowColor(const rgb_color& color)
207 {
208 	fLowColor = color;
209 }
210 
211 /*!
212 	\brief Obtains the color in the pattern at the specified coordinates
213 	\param pt Coordinates to get the color for
214 	\return Color for the coordinates
215 */
216 RGBColor
217 PatternHandler::ColorAt(const BPoint &pt) const
218 {
219 	return ColorAt(pt.x, pt.y);
220 }
221 
222 /*!
223 	\brief Obtains the color in the pattern at the specified coordinates
224 	\param x X coordinate to get the color for
225 	\param y Y coordinate to get the color for
226 	\return Color for the coordinates
227 */
228 RGBColor
229 PatternHandler::ColorAt(float x, float y) const
230 {
231 	return ColorAt(int(x), int(y));
232 }
233 
234 /*!
235 	\brief Obtains the value of the pattern at the specified coordinates
236 	\param pt Coordinates to get the value for
237 	\return Value for the coordinates - true if high, false if low.
238 */
239 bool PatternHandler::IsHighColor(const BPoint &pt) const
240 {
241 	return IsHighColor((int)pt.x, (int)pt.y);
242 }
243 
244