2 * Copyright (c) 2001-2007, Haiku, Inc.
3 * Distributed under the terms of the MIT license.
5 * Author: DarkWyrm <bpmagic@columbus.rr.com>
6 * Stephan Aßmus <superstippi@gmx.de>
9 #include "PatternHandler.h"
16 const Pattern
kSolidHigh(0xFFFFFFFFFFFFFFFFLL
);
17 const Pattern
kSolidLow((uint64
)0);
18 const Pattern
kMixedColors(0xAAAAAAAAAAAAAAAALL
);
20 const rgb_color kBlack
= (rgb_color
){ 0, 0, 0, 255 };
21 const rgb_color kWhite
= (rgb_color
){ 255, 255, 255, 255 };
24 \brief Void constructor
26 The pattern is set to B_SOLID_HIGH, high color is set to black, and
27 low color is set to white.
29 PatternHandler::PatternHandler(void)
30 : fPattern(kSolidHigh
),
35 fColorsWhenCached(ULONGLONG_MAX
)
37 memset(fOpCopyColorCache
, 255, 256 * sizeof(rgb_color
));
41 \brief Constructor initializes to given pattern
42 \param pat Pattern to use.
44 This initializes to the given pattern or B_SOLID_HIGH if the pattern
45 is NULL. High color is set to black, and low color is set to white.
47 PatternHandler::PatternHandler(const int8
* pat
)
48 : fPattern(pat
? Pattern(pat
) : Pattern(kSolidHigh
)),
53 fColorsWhenCached(ULONGLONG_MAX
)
55 memset(fOpCopyColorCache
, 255, 256 * sizeof(rgb_color
));
59 \brief Constructor initializes to given pattern
60 \param pat Pattern to use.
62 This initializes to the given pattern or B_SOLID_HIGH if the pattern
63 is NULL. High color is set to black, and low color is set to white.
65 PatternHandler::PatternHandler(const uint64
& pat
)
71 fColorsWhenCached(ULONGLONG_MAX
)
73 memset(fOpCopyColorCache
, 255, 256 * sizeof(rgb_color
));
77 \brief Constructor initializes to given pattern
78 \param pat Pattern to use.
80 This initializes to the given Pattern.
81 High color is set to black, and low color is set to white.
83 PatternHandler::PatternHandler(const Pattern
& pat
)
89 fColorsWhenCached(ULONGLONG_MAX
)
91 memset(fOpCopyColorCache
, 255, 256 * sizeof(rgb_color
));
95 \brief Constructor initializes to given PatternHandler
96 \param other PatternHandler to copy.
100 PatternHandler::PatternHandler(const PatternHandler
& other
)
101 : fPattern(other
.fPattern
),
102 fHighColor(other
.fHighColor
),
103 fLowColor(other
.fLowColor
),
104 fXOffset(other
.fXOffset
),
105 fYOffset(other
.fYOffset
),
106 fColorsWhenCached(ULONGLONG_MAX
)
108 memset(fOpCopyColorCache
, 255, 256 * sizeof(rgb_color
));
111 //! Destructor does nothing
112 PatternHandler::~PatternHandler(void)
117 \brief Sets the pattern for the handler to the one given
118 \param pat Pattern to use.
120 This initializes to the given pattern or B_SOLID_HIGH if the pattern
124 PatternHandler::SetPattern(const int8
* pat
)
129 fPattern
= kSolidHigh
;
133 \brief Sets the pattern for the handler to the one given
134 \param pat Pattern to use.
137 PatternHandler::SetPattern(const uint64
& pat
)
143 \brief Sets the pattern for the handler to the one given
144 \param pat Pattern to use.
147 PatternHandler::SetPattern(const Pattern
& pat
)
153 \brief Sets the pattern for the handler to the one given
154 \param pat R5 style pattern to use.
157 PatternHandler::SetPattern(const pattern
& pat
)
163 \brief Set the colors for the pattern to use
164 \param high High color for the handler
165 \param low Low color for the handler
168 PatternHandler::SetColors(const rgb_color
& high
, const rgb_color
& low
)
175 \brief Set the high color for the pattern to use
176 \param color High color for the handler
179 PatternHandler::SetHighColor(const rgb_color
& color
)
185 \brief Set the low color for the pattern to use
186 \param color Low color for the handler
189 PatternHandler::SetLowColor(const rgb_color
& color
)
195 \brief Obtains the color in the pattern at the specified coordinates
196 \param pt Coordinates to get the color for
197 \return Color for the coordinates
200 PatternHandler::ColorAt(const BPoint
&pt
) const
202 return ColorAt(pt
.x
, pt
.y
);
206 \brief Obtains the color in the pattern at the specified coordinates
207 \param x X coordinate to get the color for
208 \param y Y coordinate to get the color for
209 \return Color for the coordinates
212 PatternHandler::ColorAt(float x
, float y
) const
214 return ColorAt(int(x
), int(y
));
218 \brief Obtains the value of the pattern at the specified coordinates
219 \param pt Coordinates to get the value for
220 \return Value for the coordinates - true if high, false if low.
223 PatternHandler::IsHighColor(const BPoint
&pt
) const
225 return IsHighColor((int)pt
.x
, (int)pt
.y
);
229 \brief Transfers the scrolling offset of a BView to affect the pattern.
230 \param x Positive or negative horizontal offset
231 \param y Positive or negative vertical offset
234 PatternHandler::SetOffsets(int32 x
, int32 y
)
242 PatternHandler::MakeOpCopyColorCache()
244 uint64 t
= *(uint32
*)&fHighColor
;
245 uint64 colors
= (t
<< 32) | *(uint32
*)&fLowColor
;
247 if (fColorsWhenCached
== colors
)
250 fColorsWhenCached
= colors
;
252 // ramp from low color to high color
253 uint8 rA
= fLowColor
.red
;
254 uint8 gA
= fLowColor
.green
;
255 uint8 bA
= fLowColor
.blue
;
257 uint8 rB
= fHighColor
.red
;
258 uint8 gB
= fHighColor
.green
;
259 uint8 bB
= fHighColor
.blue
;
261 for (int32 i
= 0; i
< 256; i
++) {
262 // NOTE: rgb is twisted around, since this is
263 // only used as uint32 in the end
264 fOpCopyColorCache
[i
].red
= (((bB
- bA
) * i
) + (bA
<< 8)) >> 8;
265 fOpCopyColorCache
[i
].green
= (((gB
- gA
) * i
) + (gA
<< 8)) >> 8;
266 fOpCopyColorCache
[i
].blue
= (((rB
- rA
) * i
) + (rA
<< 8)) >> 8;