]> git.sesse.net Git - vlc/blob - plugins/beos/DrawingTidbits.cpp
* Header cleaning: filled all empty authors fields, added CVS $Id stuff.
[vlc] / plugins / beos / DrawingTidbits.cpp
1 /*****************************************************************************
2  * DrawingTidbits.cpp
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: DrawingTidbits.cpp,v 1.2 2001/03/21 13:42:33 sam Exp $
6  *
7  * Authors: Tony Castley <tcastley@mail.powerup.com.au>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #include <Bitmap.h>
25 #include <Debug.h>
26 #include <Screen.h>
27
28 #include "DrawingTidbits.h"
29
30 inline uchar
31 ShiftComponent(uchar component, float percent)
32 {
33         // change the color by <percent>, make sure we aren't rounding
34         // off significant bits
35         if (percent >= 1)
36                 return (uchar)(component * (2 - percent));
37         else
38                 return (uchar)(255 - percent * (255 - component));
39 }
40
41 rgb_color
42 ShiftColor(rgb_color color, float percent)
43 {
44         rgb_color result = {
45                 ShiftComponent(color.red, percent),
46                 ShiftComponent(color.green, percent),
47                 ShiftComponent(color.blue, percent),
48                 0
49         };
50         
51         return result;
52 }
53
54 static bool
55 CompareColors(const rgb_color a, const rgb_color b)
56 {
57         return a.red == b.red
58                 && a.green == b.green
59                 && a.blue == b.blue
60                 && a.alpha == b.alpha;
61 }
62
63 bool 
64 operator==(const rgb_color &a, const rgb_color &b)
65 {
66         return CompareColors(a, b);
67 }
68
69 bool 
70 operator!=(const rgb_color &a, const rgb_color &b)
71 {
72         return !CompareColors(a, b);
73 }
74
75 void
76 ReplaceColor(BBitmap *bitmap, rgb_color from, rgb_color to)
77 {
78         ASSERT(bitmap->ColorSpace() == B_COLOR_8_BIT); // other color spaces not implemented yet
79         
80         BScreen screen(B_MAIN_SCREEN_ID);
81         uint32 fromIndex = screen.IndexForColor(from);
82         uint32 toIndex = screen.IndexForColor(to); 
83         
84         uchar *bits = (uchar *)bitmap->Bits();
85         int32 bitsLength = bitmap->BitsLength();        
86         for (int32 index = 0; index < bitsLength; index++) 
87                 if (bits[index] == fromIndex)
88                         bits[index] = toIndex;
89 }
90
91 void 
92 ReplaceTransparentColor(BBitmap *bitmap, rgb_color with)
93 {
94         ASSERT(bitmap->ColorSpace() == B_COLOR_8_BIT); // other color spaces not implemented yet
95         
96         BScreen screen(B_MAIN_SCREEN_ID);
97         uint32 withIndex = screen.IndexForColor(with); 
98         
99         uchar *bits = (uchar *)bitmap->Bits();
100         int32 bitsLength = bitmap->BitsLength();        
101         for (int32 index = 0; index < bitsLength; index++) 
102                 if (bits[index] == B_TRANSPARENT_8_BIT)
103                         bits[index] = withIndex;
104 }
105