-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoastColorWindow.cpp
More file actions
705 lines (642 loc) · 23.6 KB
/
Copy pathRoastColorWindow.cpp
File metadata and controls
705 lines (642 loc) · 23.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
/* RoastColorWindow.cpp – Coffee Toolkit
*
* Author: David Masson
*/
#include "RoastColorWindow.h"
#include "Constants.h"
#include "Settings.h"
#include <Application.h>
#include <Entry.h>
#include <InterfaceDefs.h>
#include <LayoutBuilder.h>
#include <Menu.h>
#include <MenuBar.h>
#include <MenuItem.h>
#include <Message.h>
#include <Path.h>
#include <TranslationUtils.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
// -------------------------------------------------------
// RoastGaugeView
// -------------------------------------------------------
RoastGaugeView::RoastGaugeView(BRect frame)
: BView(frame, "roast_gauge", B_FOLLOW_H_CENTER | B_FOLLOW_TOP,
B_WILL_DRAW),
fAgtron(-1.0f)
{
SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
}
void RoastGaugeView::SetAgtron(float agtron)
{
fAgtron = agtron;
Invalidate();
}
void RoastGaugeView::Draw(BRect /*updateRect*/)
{
CoffeeSettings* s = CoffeeSettings::Get();
rgb_color bg = s->ThemePanelBg();
rgb_color text = s->ThemeTextColor();
rgb_color dim = s->ThemeDimTextColor();
rgb_color outline = s->ThemeOutlineColor();
BRect b = Bounds();
float barTop = 18.0f;
float barBottom = 44.0f;
float barH = barBottom - barTop;
float barLeft = b.left + 2.0f;
float barRight = b.right - 2.0f;
float barWidth = barRight - barLeft;
// Clear background
SetHighColor(bg);
FillRect(b);
auto agtronToX = [&](float ag) -> float {
float t = (ag - kAgtronMin) / (kAgtronMax - kAgtronMin);
return barLeft + t * barWidth;
};
// Gradient bar – 7 colour stops from very dark to very light roast
struct Stop { float pos; uint8 r, g, b; };
Stop stops[] = {
{ 0.00f, 10, 5, 2 },
{ 0.17f, 40, 18, 8 },
{ 0.33f, 80, 38, 15 },
{ 0.50f, 130, 72, 30 },
{ 0.67f, 175,115, 55 },
{ 0.83f, 210,165,100 },
{ 1.00f, 235,210,165 },
};
const int nStops = 7;
for (int i = 0; i < nStops - 1; i++) {
float x0 = barLeft + stops[i].pos * barWidth;
float x1 = barLeft + stops[i+1].pos * barWidth;
int steps = (int)(x1 - x0);
if (steps < 1) steps = 1;
for (int s = 0; s < steps; s++) {
float t = (float)s / steps;
uint8 r = (uint8)(stops[i].r + t*(stops[i+1].r - stops[i].r));
uint8 g = (uint8)(stops[i].g + t*(stops[i+1].g - stops[i].g));
uint8 bv = (uint8)(stops[i].b + t*(stops[i+1].b - stops[i].b));
SetHighColor(r, g, bv);
float sx = x0 + s;
StrokeLine(BPoint(sx, barTop), BPoint(sx, barBottom));
}
}
// Outline
SetHighColor(outline);
StrokeRect(BRect(barLeft, barTop, barRight, barBottom));
// Roast level band dividers and labels
struct Band { float ag; const char* name; };
Band bands[] = {
{ 25, "V.Dark" },
{ 35, "Dark" },
{ 45, "M-Dark" },
{ 55, "Medium" },
{ 65, "Light" },
{ 75, "V.Light" },
{ 95, nullptr },
};
BFont tiny; tiny.SetSize(8.5f); SetFont(&tiny);
for (int i = 0; i < 6; i++) {
if (i < 5) {
SetHighColor(100, 100, 100);
float xd = agtronToX(bands[i+1].ag);
StrokeLine(BPoint(xd, barTop), BPoint(xd, barBottom));
}
float xL = agtronToX(bands[i].ag);
float xR = agtronToX(bands[i+1].ag);
SetHighColor(220, 220, 200);
DrawString(bands[i].name,
BPoint((xL+xR)/2.0f - 14, barTop + barH*0.65f));
}
// Scale ticks
BFont small; small.SetSize(9.0f); SetFont(&small);
SetHighColor(dim);
for (int ag = 25; ag <= 95; ag += 10) {
float x = agtronToX((float)ag);
StrokeLine(BPoint(x, barBottom), BPoint(x, barBottom+4));
char lbl[8]; snprintf(lbl, sizeof(lbl), "%d", ag);
DrawString(lbl, BPoint(x-5, barBottom+14));
}
// Pointer
if (fAgtron >= kAgtronMin && fAgtron <= kAgtronMax + 1.0f) {
float x = agtronToX(fAgtron);
SetHighColor(255, 255, 255);
FillTriangle(BPoint(x, barTop-1),
BPoint(x-5, barTop-9),
BPoint(x+5, barTop-9));
SetHighColor(30, 30, 30);
StrokeTriangle(BPoint(x, barTop-1),
BPoint(x-5, barTop-9),
BPoint(x+5, barTop-9));
SetHighColor(255, 255, 255);
SetDrawingMode(B_OP_OVER);
StrokeLine(BPoint(x, barTop), BPoint(x, barBottom));
SetDrawingMode(B_OP_COPY);
char val[16]; snprintf(val, sizeof(val), "%.0f", fAgtron);
BFont norm; norm.SetSize(10.0f); SetFont(&norm);
SetHighColor(text);
DrawString(val, BPoint(x-8, barTop-11));
}
}
// -------------------------------------------------------
// ThumbView
// -------------------------------------------------------
ThumbView::ThumbView(BRect frame, BMessenger target)
: BView(frame, "thumb", B_FOLLOW_NONE,
B_WILL_DRAW | B_FRAME_EVENTS),
fBitmap(nullptr),
fTarget(target),
fHasSelection(false),
fDragging(false)
{
SetViewColor(40, 40, 40);
}
void ThumbView::SetBitmap(BBitmap* bmp)
{
fBitmap = bmp;
ClearSelection();
Invalidate();
}
void ThumbView::ClearSelection()
{
fHasSelection = false;
fDragging = false;
NotifyTarget();
Invalidate();
}
BRect ThumbView::NormalisedSelection() const
{
if (!fHasSelection) return BRect();
BRect b = Bounds();
float w = b.Width() + 1.0f;
float h = b.Height() + 1.0f;
return BRect(fSelection.left / w,
fSelection.top / h,
fSelection.right / w,
fSelection.bottom / h);
}
BRect ThumbView::OrderedRect(BPoint a, BPoint b) const
{
BRect bnd = Bounds();
auto clampX = [&](float x) {
return x < bnd.left ? bnd.left
: x > bnd.right ? bnd.right : x;
};
auto clampY = [&](float y) {
return y < bnd.top ? bnd.top
: y > bnd.bottom ? bnd.bottom : y;
};
return BRect(clampX(a.x < b.x ? a.x : b.x),
clampY(a.y < b.y ? a.y : b.y),
clampX(a.x > b.x ? a.x : b.x),
clampY(a.y > b.y ? a.y : b.y));
}
void ThumbView::NotifyTarget()
{
BMessage msg(MSG_SELECTION_CHANGED);
if (fHasSelection) {
BRect n = NormalisedSelection();
msg.AddFloat("x0", n.left);
msg.AddFloat("y0", n.top);
msg.AddFloat("x1", n.right);
msg.AddFloat("y1", n.bottom);
}
fTarget.SendMessage(&msg);
}
void ThumbView::MouseDown(BPoint where)
{
SetMouseEventMask(B_POINTER_EVENTS, B_LOCK_WINDOW_FOCUS);
fDragging = true;
fDragStart = where;
fDragCurrent = where;
fHasSelection = false;
Invalidate();
}
void ThumbView::MouseMoved(BPoint where, uint32 /*transit*/,
const BMessage* /*drag*/)
{
if (!fDragging) return;
fDragCurrent = where;
Invalidate();
}
void ThumbView::MouseUp(BPoint where)
{
if (!fDragging) return;
fDragging = false;
BRect r = OrderedRect(fDragStart, where);
if (r.Width() > 4 && r.Height() > 4) {
fSelection = r;
fHasSelection = true;
} else {
fHasSelection = false;
}
NotifyTarget();
Invalidate();
}
void ThumbView::Draw(BRect /*updateRect*/)
{
BRect b = Bounds();
if (fBitmap && fBitmap->IsValid()) {
DrawBitmapAsync(fBitmap, fBitmap->Bounds(),
b, B_FILTER_BITMAP_BILINEAR);
} else {
CoffeeSettings* s = CoffeeSettings::Get();
SetHighColor(s->ThemeDimTextColor());
BFont f; f.SetSize(11); SetFont(&f);
DrawString("No image loaded", BPoint(10, b.Height()/2));
SetHighColor(s->ThemeOutlineColor());
DrawString("Click 'Load Image' above",
BPoint(10, b.Height()/2 + 16));
}
// Draw selection overlay
BRect selRect;
if (fDragging)
selRect = OrderedRect(fDragStart, fDragCurrent);
else if (fHasSelection)
selRect = fSelection;
else
return;
// Dashed white then offset black gives static marching-ants look
const float dash = 6.0f;
auto drawDashedRect = [&](rgb_color col, float offset) {
SetHighColor(col);
SetDrawingMode(B_OP_OVER);
for (int edge = 0; edge < 2; edge++) {
float y = (edge == 0) ? selRect.top : selRect.bottom;
float x = selRect.left + offset;
bool on = true;
while (x < selRect.right) {
float xe = x + dash;
if (xe > selRect.right) xe = selRect.right;
if (on) StrokeLine(BPoint(x, y), BPoint(xe, y));
x = xe; on = !on;
}
}
for (int edge = 0; edge < 2; edge++) {
float x = (edge == 0) ? selRect.left : selRect.right;
float y = selRect.top + offset;
bool on = true;
while (y < selRect.bottom) {
float ye = y + dash;
if (ye > selRect.bottom) ye = selRect.bottom;
if (on) StrokeLine(BPoint(x, y), BPoint(x, ye));
y = ye; on = !on;
}
}
SetDrawingMode(B_OP_COPY);
};
drawDashedRect({255, 255, 255, 255}, 0.0f);
drawDashedRect({0, 0, 0, 255}, dash);
// Darken area outside selection
SetHighColor(0, 0, 0, 80);
SetDrawingMode(B_OP_ALPHA);
if (selRect.top > b.top)
FillRect(BRect(b.left, b.top, b.right, selRect.top - 1));
if (selRect.bottom < b.bottom)
FillRect(BRect(b.left, selRect.bottom + 1, b.right, b.bottom));
if (selRect.left > b.left)
FillRect(BRect(b.left, selRect.top, selRect.left - 1, selRect.bottom));
if (selRect.right < b.right)
FillRect(BRect(selRect.right+1, selRect.top, b.right, selRect.bottom));
SetDrawingMode(B_OP_COPY);
}
// -------------------------------------------------------
// RoastColorWindow
// -------------------------------------------------------
RoastColorWindow::RoastColorWindow()
: BWindow(BRect(180, 120, 720, 760), "Roast Color Analyzer",
B_TITLED_WINDOW,
B_NOT_RESIZABLE | B_AUTO_UPDATE_SIZE_LIMITS),
fFilePanel(nullptr),
fThumbBitmap(nullptr),
fSourceBitmap(nullptr)
{
fOpenBtn = new BButton("open_btn", "Load Image...",
new BMessage(MSG_ROAST_OPEN));
fFileNameView = new BStringView("fname", "No image loaded.");
fFileNameView->SetExplicitAlignment(
BAlignment(B_ALIGN_LEFT, B_ALIGN_MIDDLE));
BMessenger me(this);
fThumbView = new ThumbView(BRect(0, 0, 319, 239), me);
fThumbView->SetExplicitMinSize(BSize(320, 180));
fThumbView->SetExplicitMaxSize(BSize(320, 180));
fSelHintView = new BStringView("sel_hint",
"Drag on the image to select a sample region.");
BFont hintFont(be_plain_font);
hintFont.SetFace(B_ITALIC_FACE);
fSelHintView->SetFont(&hintFont);
fClearSelBtn = new BButton("clear_sel", "Clear Selection",
new BMessage(MSG_CLEAR_SELECTION));
fClearSelBtn->SetEnabled(false);
fGaugeView = new RoastGaugeView(BRect(0, 0, 460, 70));
fGaugeView->SetExplicitMinSize(BSize(460, 70));
fGaugeView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, 70));
fAgtronView = new BStringView("agtron_result", "");
fAgtronView->SetFont(be_bold_font);
fAgtronView->SetExplicitAlignment(
BAlignment(B_ALIGN_CENTER, B_ALIGN_MIDDLE));
BRect tvFrame(0, 0, 440, 150);
fTipsView = new BTextView(tvFrame, "roast_tips",
tvFrame.OffsetToCopy(B_ORIGIN),
B_FOLLOW_ALL, B_WILL_DRAW);
fTipsView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
fTipsView->MakeEditable(false);
fTipsView->MakeSelectable(false);
fTipsView->SetWordWrap(true);
fTipsView->SetText(
"Load a photograph of your coffee grounds or whole beans.\n\n"
"For best accuracy:\n"
" * Use ground coffee (consistent surface)\n"
" * Photograph on a neutral grey or white card\n"
" * Use even, diffuse lighting - avoid direct sunlight\n"
" * Fill most of the frame with coffee\n\n"
"Drag a selection rectangle on the preview to sample a\n"
"specific region instead of the default centre crop.");
{
CoffeeSettings* s = CoffeeSettings::Get();
fTipsView->SetViewColor(s->ThemePanelBg());
rgb_color tc = s->ThemeTextColor();
fTipsView->SetFontAndColor(0, fTipsView->TextLength(),
be_plain_font, B_FONT_ALL, &tc);
}
fTipsScroll = new BScrollView("roast_tips_scroll", fTipsView,
B_FOLLOW_ALL, 0, false, true);
fTipsScroll->SetExplicitMinSize(BSize(460, 70));
fTipsScroll->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, 100));
BMenuBar* menuBar = new BMenuBar("menubar");
BMenu* helpMenu = new BMenu("Help");
helpMenu->AddItem(new BMenuItem("About" B_UTF8_ELLIPSIS,
new BMessage(B_ABOUT_REQUESTED)));
menuBar->AddItem(helpMenu);
CoffeeSettings::BuildSettingsMenu(menuBar);
BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
.Add(menuBar)
.AddGroup(B_VERTICAL, kPad)
.SetInsets(kPad*2, kPad*2, kPad*2, kPad*2)
.AddGroup(B_HORIZONTAL, kPad)
.Add(fOpenBtn)
.Add(fFileNameView)
.End()
.Add(fThumbView)
.AddGroup(B_HORIZONTAL, kPad)
.Add(fSelHintView)
.AddGlue()
.Add(fClearSelBtn)
.End()
.Add(new BStringView("sep1", ""))
.Add(new BStringView("gauge_lbl", "Agtron roast scale:"))
.Add(fGaugeView)
.Add(fAgtronView)
.Add(new BStringView("sep2", ""))
.Add(new BStringView("tips_lbl", "Roast notes & guidance:"))
.Add(fTipsScroll)
.End();
ResizeTo(GetLayout()->PreferredSize().width + 40,
GetLayout()->PreferredSize().height);
CenterOnScreen();
}
RoastColorWindow::~RoastColorWindow()
{
delete fSourceBitmap;
delete fThumbBitmap;
delete fFilePanel;
}
void RoastColorWindow::LoadImage(const entry_ref& ref)
{
BBitmap* bmp = BTranslationUtils::GetBitmapFile(BPath(&ref).Path());
if (!bmp || !bmp->IsValid()) {
delete bmp;
fFileNameView->SetText("Error: could not load image.");
return;
}
delete fSourceBitmap;
fSourceBitmap = bmp;
BEntry entry(&ref);
char name[B_FILE_NAME_LENGTH];
entry.GetName(name);
fFileNameView->SetText(name);
// Scale thumbnail to fit 320x240, preserving aspect ratio
BRect src = fSourceBitmap->Bounds();
float sw = src.Width() + 1;
float sh = src.Height() + 1;
float scale = (sw/sh > 320.0f/240.0f) ? 320.0f/sw : 240.0f/sh;
int tw = (int)(sw * scale);
int th = (int)(sh * scale);
delete fThumbBitmap;
fThumbBitmap = new BBitmap(BRect(0, 0, tw-1, th-1), B_RGB32);
uint8* src_bits = (uint8*)fSourceBitmap->Bits();
uint8* dst_bits = (uint8*)fThumbBitmap->Bits();
int src_bpr = fSourceBitmap->BytesPerRow();
int dst_bpr = fThumbBitmap->BytesPerRow();
color_space cs = fSourceBitmap->ColorSpace();
for (int dy = 0; dy < th; dy++) {
int sy = (int)(dy * sh / th);
for (int dx = 0; dx < tw; dx++) {
int sx = (int)(dx * sw / tw);
uint8 r = 128, g = 128, bv = 128;
if (cs == B_RGB32 || cs == B_RGBA32) {
uint8* p = src_bits + sy*src_bpr + sx*4;
bv = p[0]; g = p[1]; r = p[2];
} else if (cs == B_RGB24) {
uint8* p = src_bits + sy*src_bpr + sx*3;
bv = p[0]; g = p[1]; r = p[2];
}
uint8* d = dst_bits + dy*dst_bpr + dx*4;
d[0] = bv; d[1] = g; d[2] = r; d[3] = 255;
}
}
fThumbView->SetBitmap(fThumbBitmap);
fClearSelBtn->SetEnabled(false);
Analyse();
}
float RoastColorWindow::ComputeAgtron(BBitmap* bmp, BRect normSel)
{
BRect bounds = bmp->Bounds();
int w = (int)(bounds.Width() + 1);
int h = (int)(bounds.Height() + 1);
int bpr = bmp->BytesPerRow();
color_space cs = bmp->ColorSpace();
uint8* bits = (uint8*)bmp->Bits();
int x0, y0, x1, y1;
if (normSel.IsValid() && normSel.Width() > 0 && normSel.Height() > 0) {
x0 = (int)(normSel.left * w);
y0 = (int)(normSel.top * h);
x1 = (int)(normSel.right * w);
y1 = (int)(normSel.bottom * h);
} else {
x0 = w/4; y0 = h/4;
x1 = w*3/4; y1 = h*3/4;
}
if (x0 < 0) x0 = 0;
if (x1 > w) x1 = w;
if (y0 < 0) y0 = 0;
if (y1 > h) y1 = h;
if (x0 >= x1 || y0 >= y1) return 50.0f;
double sumL = 0.0;
int count = 0;
for (int y = y0; y < y1; y++) {
for (int x = x0; x < x1; x++) {
float rf = 0, gf = 0, bf = 0;
if (cs == B_RGB32 || cs == B_RGBA32) {
uint8* p = bits + y*bpr + x*4;
bf = p[0]/255.0f; gf = p[1]/255.0f; rf = p[2]/255.0f;
} else if (cs == B_RGB24) {
uint8* p = bits + y*bpr + x*3;
bf = p[0]/255.0f; gf = p[1]/255.0f; rf = p[2]/255.0f;
} else continue;
auto lin = [](float c) -> float {
return c <= 0.04045f ? c/12.92f
: powf((c+0.055f)/1.055f, 2.4f);
};
float L = 0.2126f*lin(rf) + 0.7152f*lin(gf) + 0.0722f*lin(bf);
sumL += L;
count++;
}
}
if (count == 0) return 50.0f;
float avgL = (float)(sumL / count);
float agtron = kAgtronMin + avgL * (kAgtronMax - kAgtronMin);
if (agtron < kAgtronMin) agtron = kAgtronMin;
if (agtron > kAgtronMax) agtron = kAgtronMax;
return agtron;
}
const char* RoastColorWindow::RoastName(float ag)
{
if (ag >= 75) return "Very Light (Cinnamon / White Roast)";
if (ag >= 65) return "Light (City)";
if (ag >= 55) return "Medium (City+)";
if (ag >= 45) return "Medium-Dark (Full City / Full City+)";
if (ag >= 35) return "Dark (Vienna / French)";
return "Very Dark (Italian / Spanish)";
}
void RoastColorWindow::BuildTips(float ag, char* buf, size_t sz)
{
const char* name = RoastName(ag);
if (ag >= 75) {
snprintf(buf, sz,
"Roast level: %s\n\nVery lightly roasted coffee retains maximum "
"origin character.\n\nFlavour notes: floral, fruity, tea-like, "
"bright acidity.\nBrew tips:\n"
" * Use lower water temperature (85-90 deg C).\n"
" * Grind finer than you would for a medium roast.\n"
" * Well-suited to pour-over and Chemex methods.\n"
" * Avoid espresso - low solubility makes it difficult.", name);
} else if (ag >= 65) {
snprintf(buf, sz,
"Roast level: %s\n\nA light roast with good clarity and origin "
"expression.\n\nFlavour notes: stone fruit, citrus, caramel "
"sweetness.\nBrew tips:\n"
" * Water temperature 88-93 deg C.\n"
" * Works well with V60, Kalita Wave, AeroPress.\n"
" * Can pull as espresso with extended pre-infusion.\n"
" * Rest 7-14 days post-roast for CO2 degassing.", name);
} else if (ag >= 55) {
snprintf(buf, sz,
"Roast level: %s\n\nThe most versatile roast - balanced across "
"all brew methods.\n\nFlavour notes: chocolate, nuts, mild fruit, "
"balanced acidity.\nBrew tips:\n"
" * Water temperature 90-94 deg C.\n"
" * Excellent for espresso, filter, and immersion methods.\n"
" * Aim for 18-22%% extraction yield.\n"
" * Rest 5-10 days post-roast.", name);
} else if (ag >= 45) {
snprintf(buf, sz,
"Roast level: %s\n\nA darker roast with developed body and "
"reduced acidity.\n\nFlavour notes: dark chocolate, caramel, "
"toasted nuts.\nBrew tips:\n"
" * Water temperature 90-93 deg C.\n"
" * Ideal for espresso and French press.\n"
" * Grind slightly coarser to prevent over-extraction.\n"
" * Milk-based drinks complement this roast level well.", name);
} else if (ag >= 35) {
snprintf(buf, sz,
"Roast level: %s\n\nA dark roast with bold body and low origin "
"character.\n\nFlavour notes: bittersweet chocolate, smoky.\n"
"Brew tips:\n"
" * Water temperature 88-92 deg C.\n"
" * Well-suited to espresso and moka pot.\n"
" * Use a coarser grind to prevent bitterness.\n"
" * Can stand up to milk and sugar.", name);
} else {
snprintf(buf, sz,
"Roast level: %s\n\nA very dark roast - roast character "
"dominates entirely.\n\nFlavour notes: charred, smoky, ash.\n"
"Brew tips:\n"
" * Water temperature 85-90 deg C.\n"
" * Best suited to espresso only.\n"
" * Use a coarser grind - oils clog finer settings.\n"
" * Origin flavour is largely absent at this level.", name);
}
}
void RoastColorWindow::Analyse()
{
if (!fSourceBitmap || !fSourceBitmap->IsValid()) return;
BRect normSel;
if (fThumbView->HasSelection())
normSel = fThumbView->NormalisedSelection();
float agtron = ComputeAgtron(fSourceBitmap, normSel);
fGaugeView->SetAgtron(agtron);
char label[192];
snprintf(label, sizeof(label),
"Agtron: %.0f -- %s [%s]",
agtron, RoastName(agtron),
fThumbView->HasSelection() ? "custom selection" : "centre region");
fAgtronView->SetText(label);
char tips[2048];
BuildTips(agtron, tips, sizeof(tips));
fTipsView->SetText(tips);
{
rgb_color tc = CoffeeSettings::Get()->ThemeTextColor();
fTipsView->SetFontAndColor(0, fTipsView->TextLength(),
be_plain_font, B_FONT_ALL, &tc);
}
}
void RoastColorWindow::MessageReceived(BMessage* msg)
{
switch (msg->what) {
case MSG_ROAST_OPEN: {
if (!fFilePanel) {
BMessenger me(this);
fFilePanel = new BFilePanel(B_OPEN_PANEL, &me, nullptr,
B_FILE_NODE, false,
new BMessage(MSG_ROAST_REFS));
}
fFilePanel->Show();
break;
}
case MSG_ROAST_REFS: {
entry_ref ref;
if (msg->FindRef("refs", &ref) == B_OK)
LoadImage(ref);
break;
}
case MSG_SELECTION_CHANGED: {
fClearSelBtn->SetEnabled(msg->HasFloat("x0"));
if (fSourceBitmap)
Analyse();
break;
}
case MSG_CLEAR_SELECTION: {
fThumbView->ClearSelection();
fClearSelBtn->SetEnabled(false);
if (fSourceBitmap)
Analyse();
break;
}
case MSG_THEME_CHANGED: {
CoffeeSettings* s = CoffeeSettings::Get();
rgb_color bg = s->ThemePanelBg();
rgb_color tc = s->ThemeTextColor();
fTipsView->SetViewColor(bg);
fTipsView->SetFontAndColor(0, fTipsView->TextLength(),
be_plain_font, B_FONT_ALL, &tc);
fTipsView->Invalidate();
fGaugeView->Invalidate();
break;
}
default:
if (CoffeeSettings::Get()->HandleSettingsMessage(msg))
break;
BWindow::MessageReceived(msg);
}
}