xref: /haiku/src/add-ons/screen_savers/gravity/RainbowItem.cpp (revision 02354704729d38c3b078c696adc1bbbd33cbcf72)
1 /*
2  * Copyright 2016 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		John Scipione, jscipione@gmail.com
7  */
8 
9 
10 #include "RainbowItem.h"
11 
12 #include <math.h>
13 
14 #include <ControlLook.h>
15 #include <GradientLinear.h>
16 #include <InterfaceDefs.h>
17 #include <View.h>
18 
19 
20 // golden ratio
21 #ifdef M_PHI
22 #	undef M_PHI
23 #endif
24 #define M_PHI 1.61803398874989484820
25 
26 
27 //	#pragma mark - RainbowItem
28 
29 
30 RainbowItem::RainbowItem(const char* string)
31 	:
32 	BStringItem(string, 0, false)
33 {
34 }
35 
36 
37 void
38 RainbowItem::DrawItem(BView* owner, BRect frame, bool complete)
39 {
40 	rgb_color highColor = owner->HighColor();
41 	rgb_color lowColor = owner->LowColor();
42 
43 	if (IsSelected() || complete) {
44 		if (IsSelected()) {
45 			owner->SetHighUIColor(B_LIST_SELECTED_BACKGROUND_COLOR);
46 			owner->SetLowColor(owner->HighColor());
47 		} else
48 			owner->SetHighColor(lowColor);
49 
50 		owner->FillRect(frame);
51 	}
52 
53 	float spacer = ceilf(be_control_look->DefaultItemSpacing() / 2);
54 
55 	BRect colorRect(frame);
56 	colorRect.InsetBy(2.0f, 2.0f);
57 	colorRect.left += spacer;
58 	colorRect.right = colorRect.left + floorf(colorRect.Height() * M_PHI);
59 
60 	// draw the rainbow
61 	BGradientLinear gradient;
62 	gradient.AddColor((rgb_color){ 255, 65,  54  }, 0);   // red
63 	gradient.AddColor((rgb_color){ 255, 133, 27  }, 60);  // orange
64 	gradient.AddColor((rgb_color){ 255, 220, 0   }, 102); // yellow
65 	gradient.AddColor((rgb_color){ 46,  204, 64  }, 153); // green
66 	gradient.AddColor((rgb_color){ 0,   116, 217 }, 195); // blue
67 	// indigo ;)
68 	gradient.AddColor((rgb_color){ 177, 13,  201 }, 255); // violet
69 	gradient.SetStart(colorRect.LeftTop());
70 	gradient.SetEnd(colorRect.RightBottom());
71 	owner->FillRect(colorRect, gradient);
72 
73 	// draw the border
74 	owner->SetHighUIColor(B_CONTROL_BORDER_COLOR);
75 	owner->StrokeRect(colorRect);
76 
77 	// draw the string
78 	owner->MovePenTo(colorRect.right + spacer, frame.top + BaselineOffset());
79 
80 	if (!IsEnabled()) {
81 		rgb_color textColor = ui_color(B_LIST_ITEM_TEXT_COLOR);
82 		if (textColor.red + textColor.green + textColor.blue > 128 * 3)
83 			owner->SetHighColor(tint_color(textColor, B_DARKEN_2_TINT));
84 		else
85 			owner->SetHighColor(tint_color(textColor, B_LIGHTEN_2_TINT));
86 	} else {
87 		if (IsSelected())
88 			owner->SetHighUIColor(B_LIST_SELECTED_ITEM_TEXT_COLOR);
89 		else
90 			owner->SetHighUIColor(B_LIST_ITEM_TEXT_COLOR);
91 	}
92 
93 	owner->DrawString(Text());
94 
95 	owner->SetHighColor(highColor);
96 	owner->SetLowColor(lowColor);
97 }
98