1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: DrawingTidbits.cpp,v 1.1 2002/08/04 17:23:43 sam Exp $
7 * Authors: Tony Castley <tcastley@mail.powerup.com.au>
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.
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.
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 *****************************************************************************/
28 #include "DrawingTidbits.h"
31 ShiftComponent(uchar component, float percent)
33 // change the color by <percent>, make sure we aren't rounding
34 // off significant bits
36 return (uchar)(component * (2 - percent));
38 return (uchar)(255 - percent * (255 - component));
42 ShiftColor(rgb_color color, float percent)
45 ShiftComponent(color.red, percent),
46 ShiftComponent(color.green, percent),
47 ShiftComponent(color.blue, percent),
55 CompareColors(const rgb_color a, const rgb_color b)
60 && a.alpha == b.alpha;
64 operator==(const rgb_color &a, const rgb_color &b)
66 return CompareColors(a, b);
70 operator!=(const rgb_color &a, const rgb_color &b)
72 return !CompareColors(a, b);
76 ReplaceColor(BBitmap *bitmap, rgb_color from, rgb_color to)
78 ASSERT(bitmap->ColorSpace() == B_COLOR_8_BIT); // other color spaces not implemented yet
80 BScreen screen(B_MAIN_SCREEN_ID);
81 uint32 fromIndex = screen.IndexForColor(from);
82 uint32 toIndex = screen.IndexForColor(to);
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;
92 ReplaceTransparentColor(BBitmap *bitmap, rgb_color with)
94 ASSERT(bitmap->ColorSpace() == B_COLOR_8_BIT); // other color spaces not implemented yet
96 BScreen screen(B_MAIN_SCREEN_ID);
97 uint32 withIndex = screen.IndexForColor(with);
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;