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