Ticket #9769: PaletteView.cc

File PaletteView.cc, 4.7 KB (added by scanty, 8 years ago)

PaletteView, show a NTSC palette

Line 
1
2#include <Menu.h>
3
4#include "PaletteView.h"
5#include "Palette.h"
6
7PaletteView::PaletteView (BRect frame,int32 numcolors, int32 swatchSize)
8 : BView (frame, "palette", B_FOLLOW_NONE, B_WILL_DRAW),
9 fSwatchSize(swatchSize),
10 fColors(numcolors),
11 fPalette(new rgb_color[fColors])
12{
13
14 rgb_color_t *ntscPalette = Palette::NTSCPalette(
15 Palette::default_saturation,
16 Palette::default_hue,
17 Palette::default_contrast,
18 Palette::default_brightness,
19 Palette::default_gamma);
20 for (int32 i = 0; i < 64; i++) {
21 fPalette[i].red = ntscPalette[i].r;
22 fPalette[i].green = ntscPalette[i].g;
23 fPalette[i].blue = ntscPalette[i].b;
24 }
25
26
27
28 SetPalette(fPalette);
29}
30
31
32PaletteView::~PaletteView()
33{
34}
35
36
37void
38PaletteView::AttachedToWindow (void)
39{
40 SetViewColor (ui_color(B_PANEL_BACKGROUND_COLOR));
41
42 BMenu *mnuHue = new BMenu("Hue");
43 BMenu *mnuSat = new BMenu("Saturation");
44 BMenu *mnuContrast = new BMenu("Contrast");
45 BMenu *mnuBrightness = new BMenu("Brightness");
46 BMenu *mnuGamma = new BMenu("Gamma");
47
48 BRect r (18, 128, 110, 32);
49 fHueMenu = new BMenuField(r, "_hue_menu", "Hue", mnuHue);
50
51 r.Set(158, 128, 302, 32);
52 fSaturation = new BMenuField(r, "_sat_menu", "Saturation", mnuSat);
53
54 r.Set(18, 156, 138, 228);
55 fContrast = new BMenuField(r, "_contrast_menu", "Contrast", mnuContrast);
56
57 r.Set(158, 156, 302, 402);
58 fBrightness = new BMenuField(r, "_bright_menu", "Brightness", mnuBrightness);
59
60
61 AddChild(fHueMenu);
62 AddChild (fSaturation);
63 AddChild(fContrast);
64 AddChild(fBrightness);
65}
66
67
68#if 0
69void
70PaletteView::MessageReceived (BMessage *message)
71{
72 if (message->what == 'TINT') {
73 cout << "EF YOU SEE CAY" << endl;
74 }
75
76 BView::MessageReceived(message);
77}
78#endif
79
80void
81PaletteView::Draw (BRect frame)
82{
83 rgb_color_t *ntscPalette = Palette::NTSCPalette(
84 Palette::default_saturation,
85 Palette::default_hue,
86 Palette::default_contrast,
87 Palette::default_brightness,
88 Palette::default_gamma);
89 for (int32 i = 0; i < 64; i++) {
90 fPalette[i].red = ntscPalette[i].r;
91 fPalette[i].green = ntscPalette[i].g;
92 fPalette[i].blue = ntscPalette[i].b;
93 }
94
95 DrawSwatchMatrix (BPoint(16, 16), fSwatchSize, 32, 4);
96 DrawIndexes();
97}
98
99
100void
101PaletteView::SetPalette (rgb_color *palette)
102{
103 rgb_color_t *ntscPalette = Palette::NTSCPalette(
104 Palette::default_saturation,
105 Palette::default_hue,
106 Palette::default_contrast,
107 Palette::default_brightness,
108 Palette::default_gamma);
109 for (int32 i = 0; i < 64; i++) {
110 fPalette[i].red = ntscPalette[i].r;
111 fPalette[i].green = ntscPalette[i].g;
112 fPalette[i].blue = ntscPalette[i].b;
113 }
114}
115
116
117void
118PaletteView::DrawSwatch (BPoint where, rgb_color fill)
119{
120 rgb_color const no_tint = ui_color(B_PANEL_BACKGROUND_COLOR);
121 rgb_color const lightenmax = tint_color(no_tint, B_LIGHTEN_MAX_TINT);
122 rgb_color const darken1 = tint_color(no_tint, B_DARKEN_1_TINT);
123 rgb_color const darken4 = tint_color(no_tint, B_DARKEN_4_TINT);
124
125 BRect rect (where.x, where.y, where.x+fSwatchSize, where.y+fSwatchSize);
126
127 SetHighColor (darken1);
128 StrokeLine (rect.LeftBottom(), rect.LeftTop());
129 StrokeLine (rect.LeftTop(), rect.RightTop());
130 SetHighColor (lightenmax);
131 StrokeLine (BPoint(rect.left + 1.0f, rect.bottom), rect.RightBottom());
132 StrokeLine (rect.RightBottom(), BPoint(rect.right, rect.top + 1.0f));
133 rect.InsetBy (1, 1);
134
135 SetHighColor (darken4);
136 StrokeLine (rect.LeftBottom(), rect.LeftTop());
137 StrokeLine (rect.LeftTop(), rect.RightTop());
138 SetHighColor (no_tint);
139 StrokeLine (BPoint(rect.left + 1.0f, rect.bottom), rect.RightBottom());
140 StrokeLine (rect.RightBottom(), BPoint(rect.right, rect.top + 1.0f));
141
142 rect.InsetBy (1,1);
143
144
145 rgb_color src = fill;
146 rgb_color dst;
147
148 dst.red = src.red;
149 dst.green = src.green;
150 dst.blue = src.blue;
151
152 SetHighColor (dst);
153 FillRect (rect);
154}
155
156
157void
158PaletteView::DrawSwatchRow (BPoint start, int32 size, int32 rowlen)
159{
160 if (fPalette == NULL || size <= 0 || rowlen <= 0) {
161 return;
162 }
163
164 for (int32_t i = 0; i < rowlen; i++) {
165 DrawSwatch (start, fPalette[i]);
166 start.x += size+4;
167 }
168
169}
170
171void
172PaletteView::DrawSwatchMatrix (BPoint start, int32 size, int32 ncols, int32 nrows)
173{
174 if (nrows <= 0 || size <= 0 || fPalette == NULL) {
175 return;
176 }
177
178 for (int32_t y = 0; y < nrows; y++) {
179 DrawSwatchRow (start, size, ncols);
180 start.y += size+4;
181 fPalette += nrows * sizeof(rgb_color);
182 }
183}
184
185
186void
187PaletteView::DrawIndexes (void)
188{
189 char const nybbles[] = "0123456789ABCDEF";
190
191 // y
192 BPoint p(4, 28);
193 for (int32_t i = 0; i < 4; i++) {
194 SetHighColor(0,0,0);
195 SetFont(be_fixed_font);
196 DrawChar(nybbles[i], p);
197 p.y += (fSwatchSize+4);
198 }
199
200 // x
201 p.Set(20, 110);
202 for (int32_t i = 0; i < 16; i++) {
203 SetHighColor (0, 0, 0);
204 SetFont (be_fixed_font);
205 DrawChar(nybbles[i], p);
206 p.x += fSwatchSize+4;
207 }
208}