xref: /haiku/src/apps/soundrecorder/DrawingTidbits.cpp (revision d3d8b26997fac34a84981e6d2b649521de2cc45a)
1 /*
2  * Copyright 2005, Jérôme Duval. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Inspired by SoundCapture from Be newsletter (Media Kit Basics: Consumers and Producers)
6  */
7 
8 #include <Bitmap.h>
9 #include <Debug.h>
10 #include <Screen.h>
11 
12 #include "DrawingTidbits.h"
13 
14 rgb_color
15 ShiftColor(rgb_color color, float percent)
16 {
17 	rgb_color result = {
18 		ShiftComponent(color.red, percent),
19 		ShiftComponent(color.green, percent),
20 		ShiftComponent(color.blue, percent),
21 		0
22 	};
23 
24 	return result;
25 }
26 
27 static bool
28 CompareColors(const rgb_color a, const rgb_color b)
29 {
30 	return a.red == b.red
31 		&& a.green == b.green
32 		&& a.blue == b.blue
33 		&& a.alpha == b.alpha;
34 }
35 
36 bool
37 operator==(const rgb_color &a, const rgb_color &b)
38 {
39 	return CompareColors(a, b);
40 }
41 
42 bool
43 operator!=(const rgb_color &a, const rgb_color &b)
44 {
45 	return !CompareColors(a, b);
46 }
47 
48 void
49 ReplaceColor(BBitmap *bitmap, rgb_color from, rgb_color to)
50 {
51 	ASSERT(bitmap->ColorSpace() == B_CMAP8); // other color spaces not implemented yet
52 
53 	BScreen screen(B_MAIN_SCREEN_ID);
54 	uint32 fromIndex = screen.IndexForColor(from);
55 	uint32 toIndex = screen.IndexForColor(to);
56 
57 	uchar *bits = (uchar *)bitmap->Bits();
58 	int32 bitsLength = bitmap->BitsLength();
59 	for (int32 index = 0; index < bitsLength; index++)
60 		if (bits[index] == fromIndex)
61 			bits[index] = toIndex;
62 }
63 
64 void
65 ReplaceTransparentColor(BBitmap *bitmap, rgb_color with)
66 {
67 	ASSERT(bitmap->ColorSpace() == B_CMAP8); // other color spaces not implemented yet
68 
69 	BScreen screen(B_MAIN_SCREEN_ID);
70 	uint8 withIndex = screen.IndexForColor(with);
71 
72 	uchar *bits = (uchar *)bitmap->Bits();
73 	int32 bitsLength = bitmap->BitsLength();
74 	for (int32 index = 0; index < bitsLength; index++)
75 		if (bits[index] == B_TRANSPARENT_8_BIT)
76 			bits[index] = withIndex;
77 }
78 
79