xref: /haiku/src/preferences/appearance/ColorPreview.cpp (revision 220d04022750f40f8bac8f01fa551211e28d04f2)
1 /*
2  * Copyright 2002-2013 Haiku Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		DarkWyrm, darkwyrm@earthlink.net
7  *		John Scipione, jscipione@gmail.com
8  */
9 
10 
11 #include "ColorPreview.h"
12 
13 
14 ColorPreview::ColorPreview(BRect frame, BMessage* message,
15 	uint32 resizingMode, uint32 flags)
16 	:
17 	BView(frame,"ColorPreview", resizingMode, flags | B_WILL_DRAW)
18 {
19 	SetViewColor(B_TRANSPARENT_COLOR);
20 	SetLowColor(0, 0, 0);
21 	invoker = new BInvoker(message, this);
22 	disabledcol.red = 128;
23 	disabledcol.green = 128;
24 	disabledcol.blue = 128;
25 	disabledcol.alpha = 255;
26 	is_enabled = true;
27 	is_rect = true;
28 }
29 
30 
31 ColorPreview::~ColorPreview(void)
32 {
33 	delete invoker;
34 }
35 
36 
37 void
38 ColorPreview::Draw(BRect update)
39 {
40 	rgb_color color;
41 	if (is_enabled)
42 		color = currentcol;
43 	else
44 		color = disabledcol;
45 
46 	if (is_rect) {
47 		if (is_enabled) {
48 			rgb_color background = ui_color(B_PANEL_BACKGROUND_COLOR);
49 			rgb_color shadow = tint_color(background, B_DARKEN_1_TINT);
50 			rgb_color darkShadow = tint_color(background, B_DARKEN_3_TINT);
51 			rgb_color light = tint_color(background, B_LIGHTEN_MAX_TINT);
52 
53 			BRect bounds(Bounds());
54 
55 			BeginLineArray(4);
56 			AddLine(BPoint(bounds.left, bounds.bottom),
57 			BPoint(bounds.left, bounds.top), shadow);
58 			AddLine(BPoint(bounds.left + 1.0, bounds.top),
59 			BPoint(bounds.right, bounds.top), shadow);
60 			AddLine(BPoint(bounds.right, bounds.top + 1.0),
61 			BPoint(bounds.right, bounds.bottom), light);
62 			AddLine(BPoint(bounds.right - 1.0, bounds.bottom),
63 			BPoint(bounds.left + 1.0, bounds.bottom), light);
64 			EndLineArray();
65 			bounds.InsetBy(1.0, 1.0);
66 
67 			BeginLineArray(4);
68 			AddLine(BPoint(bounds.left, bounds.bottom),
69 			BPoint(bounds.left, bounds.top), darkShadow);
70 			AddLine(BPoint(bounds.left + 1.0, bounds.top),
71 			BPoint(bounds.right, bounds.top), darkShadow);
72 			AddLine(BPoint(bounds.right, bounds.top + 1.0),
73 			BPoint(bounds.right, bounds.bottom), background);
74 			AddLine(BPoint(bounds.right - 1.0, bounds.bottom),
75 			BPoint(bounds.left + 1.0, bounds.bottom), background);
76 			EndLineArray();
77 			bounds.InsetBy(1.0, 1.0);
78 
79 			SetHighColor(color);
80 			FillRect(bounds);
81 		} else {
82 			SetHighColor(color);
83 			FillRect(Bounds());
84 		}
85 	} else {
86 		// fill background
87 		SetHighColor(ui_color(B_PANEL_BACKGROUND_COLOR));
88 		FillRect(update);
89 
90 		SetHighColor(color);
91 		FillEllipse(Bounds());
92 		if (is_enabled)
93 			StrokeEllipse(Bounds(), B_SOLID_LOW);
94 	}
95 }
96 
97 
98 void
99 ColorPreview::MessageReceived(BMessage* message)
100 {
101 	// If we received a dropped message, see if it contains color data
102 	if (message->WasDropped()) {
103 		rgb_color* col;
104 		uint8* ptr;
105 		ssize_t size;
106 		if (message->FindData("RGBColor", (type_code)'RGBC',
107 				(const void**)&ptr,&size) == B_OK) {
108 			col = (rgb_color*)ptr;
109 			SetHighColor(*col);
110 		}
111 	}
112 
113 	BView::MessageReceived(message);
114 }
115 
116 
117 void
118 ColorPreview::SetEnabled(bool value)
119 {
120 	if (is_enabled != value) {
121 		is_enabled = value;
122 		Invalidate();
123 	}
124 }
125 
126 
127 void
128 ColorPreview::SetTarget(BHandler* target)
129 {
130 	invoker->SetTarget(target);
131 }
132 
133 
134 rgb_color
135 ColorPreview::Color(void) const
136 {
137 	return currentcol;
138 }
139 
140 
141 void
142 ColorPreview::SetColor(rgb_color col)
143 {
144 	SetHighColor(col);
145 	currentcol = col;
146 	Draw(Bounds());
147 	invoker->Invoke();
148 }
149 
150 
151 void
152 ColorPreview::SetColor(uint8 r,uint8 g, uint8 b)
153 {
154 	SetHighColor(r,g,b);
155 	currentcol.red = r;
156 	currentcol.green = g;
157 	currentcol.blue = b;
158 	Draw(Bounds());
159 	invoker->Invoke();
160 }
161 
162 
163 void
164 ColorPreview::SetMode(bool is_rectangle)
165 {
166 	is_rect = is_rectangle;
167 }
168