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
|
/*
* This file is part of RawTherapee.
*
* Copyright (c) 2004-2010 Gabor Horvath <hgabor@rawtherapee.com>
*
* RawTherapee is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RawTherapee is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RawTherapee. If not, see <https://www.gnu.org/licenses/>.
*/
#include "lockablecolorpicker.h"
#include "options.h"
#include "rtengine/color.h"
#include "rtengine/improcfun.h"
#include "rtengine/rt_math.h"
#include "rtengine/utils.h"
#include "imagearea.h"
#include "multilangmgr.h"
#include "navigator.h"
LockableColorPicker::LockableColorPicker (CropWindow* cropWindow, rtengine::procparams::ColorManagementParams *color_management_params)
: cropWindow(cropWindow), displayedValues(ColorPickerType::RGB), position(0, 0), size(Size::S15),
color_management_params(color_management_params), validity(Validity::OUTSIDE),
r(0.f), g(0.f), b(0.f), rpreview(0.f), gpreview(0.f), bpreview(0.f), hue(0.f), sat(0.f), val(0.f), L(0.f), a(0.f), bb(0.f)
{}
void LockableColorPicker::updateBackBuffer ()
{
int newW, newH;
// -------------------- setting some key constants ---------------------
constexpr double circlePadding = 3.0; // keep this value odd
constexpr double opacity = 0.62;
// ---------------------------------------------------------------------
if (validity == Validity::INSIDE) {
Gtk::DrawingArea *iArea = cropWindow->getImageArea();
Glib::RefPtr<Pango::Context> pangoContext = iArea->get_pango_context ();
Pango::FontDescription fontd = iArea->get_style_context()->get_font();
const auto& options = App::get().options();
// set font family and size
fontd.set_family(options.CPFontFamily == "default" ? "sans" : options.CPFontFamily);
const int fontSize = options.CPFontFamily == "default" ? 8 : options.CPFontSize; // pt
// Non-absolute size is defined in "Pango units" and shall be multiplied by
// Pango::SCALE from "pt":
fontd.set_size (fontSize * Pango::SCALE);
fontd.set_weight(Pango::WEIGHT_NORMAL);
pangoContext->set_font_description (fontd);
Glib::RefPtr<Pango::Layout> layout[3][2];
Glib::ustring s1, s2, s3;
PointerMotionListener* navigator = cropWindow->getPointerMotionListener ();
switch (displayedValues) {
case ColorPickerType::RGB:
navigator->getRGBText ((int)(r*255.f), (int)(g*255.f), (int)(b*255.f), s1, s2, s3);
layout[0][0] = iArea->create_pango_layout(M("NAVIGATOR_R"));
layout[0][1] = iArea->create_pango_layout(s1);
layout[1][0] = iArea->create_pango_layout(M("NAVIGATOR_G"));
layout[1][1] = iArea->create_pango_layout(s2);
layout[2][0] = iArea->create_pango_layout(M("NAVIGATOR_B"));
layout[2][1] = iArea->create_pango_layout(s3);
break;
case ColorPickerType::HSV:
navigator->getHSVText (hue, sat, val, s1, s2, s3);
layout[0][0] = iArea->create_pango_layout(M("NAVIGATOR_H"));
layout[0][1] = iArea->create_pango_layout(s1);
layout[1][0] = iArea->create_pango_layout(M("NAVIGATOR_S"));
layout[1][1] = iArea->create_pango_layout(s2);
layout[2][0] = iArea->create_pango_layout(M("NAVIGATOR_V"));
layout[2][1] = iArea->create_pango_layout(s3);
break;
case ColorPickerType::LAB:
default:
navigator->getLABText (L, a, bb, s1, s2, s3);
layout[0][0] = iArea->create_pango_layout(M("NAVIGATOR_LAB_L"));
layout[0][1] = iArea->create_pango_layout(s1);
layout[1][0] = iArea->create_pango_layout(M("NAVIGATOR_LAB_A"));
layout[1][1] = iArea->create_pango_layout(s2);
layout[2][0] = iArea->create_pango_layout(M("NAVIGATOR_LAB_B"));
layout[2][1] = iArea->create_pango_layout(s3);
}
int w00, w01, w10, w11, w20, w21, h00, h01, h10, h11, h20, h21;
layout[0][0]->get_pixel_size(w00, h00);
layout[1][0]->get_pixel_size(w10, h10);
layout[2][0]->get_pixel_size(w20, h20);
layout[0][1]->get_pixel_size(w01, h01);
layout[1][1]->get_pixel_size(w11, h11);
layout[2][1]->get_pixel_size(w21, h21);
int maxWCol0 = rtengine::max(w00, w10, w20);
int maxWCol1 = rtengine::max(w01, w11, w21);
int maxHRow0 = rtengine::max(h00, h01);
int maxHRow1 = rtengine::max(h10, h11);
int maxHRow2 = rtengine::max(h20, h21);
// -------------------- setting some key constants ---------------------
constexpr int textPadding = 3;
const int textWidth = maxWCol0 + maxWCol1 + textPadding;
const int textHeight = maxHRow0 + maxHRow1 + maxHRow2 + 2*textPadding;
// ---------------------------------------------------------------------
newW = rtengine::max<int>((int)size + 2 * circlePadding, textWidth + 2 * textPadding);
newH = (int)size + 2 * circlePadding + textHeight + 2 * textPadding;
setDrawRectangle(Cairo::FORMAT_ARGB32, 0, 0, newW, newH, true);
Cairo::RefPtr<Cairo::Context> bbcr = BackBuffer::getContext();
// cleaning the back buffer
bbcr->set_source_rgba (0., 0., 0., 0.);
bbcr->set_operator (Cairo::OPERATOR_CLEAR);
bbcr->paint ();
bbcr->set_operator (Cairo::OPERATOR_OVER);
bbcr->set_antialias (Cairo::ANTIALIAS_SUBPIXEL);
bbcr->set_line_width (0.);
double center = static_cast<double>(size) / 2.0 + circlePadding;
// black background of the whole color picker
bbcr->set_line_width (0.);
bbcr->set_source_rgba (0., 0., 0., opacity);
bbcr->arc_negative (center, center, center, 0., rtengine::RT_PI);
bbcr->line_to (0, 2. * center + textHeight);
bbcr->arc_negative (2. * textPadding, 2. * center + textHeight, 2. * textPadding, rtengine::RT_PI, rtengine::RT_PI / 2.);
bbcr->line_to (textWidth, 2. * center + textHeight + 2. * textPadding);
bbcr->arc_negative (textWidth, 2. * center + textHeight, 2. * textPadding, rtengine::RT_PI / 2., 0.);
bbcr->line_to (textWidth + 2. * textPadding, 2. * center + 2. * textPadding);
bbcr->arc_negative (textWidth, 2. * center + 2. * textPadding, 2. * textPadding, 0., rtengine::RT_PI * 1.5);
bbcr->line_to (2. * center, 2. * center);
bbcr->close_path();
bbcr->set_line_join (Cairo::LINE_JOIN_BEVEL);
bbcr->set_line_cap (Cairo::LINE_CAP_SQUARE);
bbcr->fill ();
// light grey circle around the color mark
bbcr->arc (center, center, center - circlePadding / 2., 0., 2. * (double)rtengine::RT_PI);
bbcr->set_source_rgb (0.75, 0.75, 0.75);
bbcr->set_line_width (circlePadding - 2.);
bbcr->stroke ();
// spot disc with picked color
bbcr->arc (center, center, center - circlePadding, 0., 2. * (double)rtengine::RT_PI);
bbcr->set_source_rgb (rpreview, gpreview, bpreview); // <- set the picker color here
bbcr->set_line_width (0.);
bbcr->fill();
// adding the font
bbcr->set_line_width (0.);
bbcr->set_line_join (Cairo::LINE_JOIN_ROUND);
bbcr->set_line_cap (Cairo::LINE_CAP_ROUND);
bbcr->set_source_rgb (1., 1., 1.);
double txtOffsetX = textPadding;
double txtOffsetY = (double)size + 2. * circlePadding + textPadding;
switch (iArea->get_direction()) {
case Gtk::TEXT_DIR_RTL:
bbcr->move_to (txtOffsetX , txtOffsetY);
layout[0][1]->add_to_cairo_context (bbcr);
bbcr->fill ();
bbcr->move_to (txtOffsetX + maxWCol1 + textPadding, txtOffsetY);
layout[0][0]->add_to_cairo_context (bbcr);
bbcr->fill ();
bbcr->move_to (txtOffsetX , txtOffsetY + maxHRow0 + textPadding);
layout[1][1]->add_to_cairo_context (bbcr);
bbcr->fill ();
bbcr->move_to (txtOffsetX + maxWCol1 + textPadding, txtOffsetY + maxHRow0 + textPadding);
layout[1][0]->add_to_cairo_context (bbcr);
bbcr->fill ();
bbcr->move_to (txtOffsetX , txtOffsetY + maxHRow0 + maxHRow1 + 2*textPadding);
layout[2][1]->add_to_cairo_context (bbcr);
bbcr->fill ();
bbcr->move_to (txtOffsetX + maxWCol1 + textPadding, txtOffsetY + maxHRow0 + maxHRow1 + 2*textPadding);
layout[2][0]->add_to_cairo_context (bbcr);
bbcr->fill ();
break;
default:
bbcr->move_to (txtOffsetX , txtOffsetY);
layout[0][0]->add_to_cairo_context (bbcr);
bbcr->fill ();
bbcr->move_to (txtOffsetX + maxWCol0 + textPadding, txtOffsetY);
layout[0][1]->add_to_cairo_context (bbcr);
bbcr->fill ();
bbcr->move_to (txtOffsetX , txtOffsetY + maxHRow0 + textPadding);
layout[1][0]->add_to_cairo_context (bbcr);
bbcr->fill ();
bbcr->move_to (txtOffsetX + maxWCol0 + textPadding, txtOffsetY + maxHRow0 + textPadding);
layout[1][1]->add_to_cairo_context (bbcr);
bbcr->fill ();
bbcr->move_to (txtOffsetX , txtOffsetY + maxHRow0 + maxHRow1 + 2*textPadding);
layout[2][0]->add_to_cairo_context (bbcr);
bbcr->fill ();
bbcr->move_to (txtOffsetX + maxWCol0 + textPadding, txtOffsetY + maxHRow0 + maxHRow1 + 2*textPadding);
layout[2][1]->add_to_cairo_context (bbcr);
bbcr->fill ();
}
anchorOffset.set (center, center);
setDirty (false);
} else if (validity == Validity::CROSSING) {
newH = newW = (int)size + 2 * circlePadding;
setDrawRectangle(Cairo::FORMAT_ARGB32, 0, 0, newW, newH, true);
Cairo::RefPtr<Cairo::Context> bbcr = BackBuffer::getContext();
// cleaning the back buffer
bbcr->set_source_rgba (0., 0., 0., 0.);
bbcr->set_operator (Cairo::OPERATOR_CLEAR);
bbcr->paint ();
bbcr->set_operator (Cairo::OPERATOR_OVER);
bbcr->set_antialias(Cairo::ANTIALIAS_SUBPIXEL);
double center = static_cast<double>(size) / 2. + circlePadding;
// light grey circle around the color mark
bbcr->arc (center, center, center - circlePadding / 2., 0., 2. * (double)rtengine::RT_PI);
bbcr->set_source_rgba (0., 0., 0., opacity);
bbcr->set_line_width(circlePadding);
bbcr->stroke_preserve();
bbcr->set_source_rgb (0.75, 0.75, 0.75);
bbcr->set_line_width (circlePadding - 2.);
bbcr->stroke ();
anchorOffset.set (center, center);
setDirty (false);
}
}
void LockableColorPicker::draw (const Cairo::RefPtr<Cairo::Context> &cr)
{
if (validity == Validity::OUTSIDE) {
return;
}
if (isDirty()) {
updateBackBuffer();
}
int px, py;
cropWindow->imageCoordToScreen(position.x, position.y, px, py);
setDestPosition(px - anchorOffset.x, py - anchorOffset.y);
copySurface(cr);
}
void LockableColorPicker::setPosition (const rtengine::Coord &newPos)
{
// we're not checking bounds here, this will be done at rendering time
position = newPos;
}
void LockableColorPicker::setRGB (const float R, const float G, const float B, const float previewR, const float previewG, const float previewB)
{
if (r==R && g==G && b==B) {
return;
}
r = R;
g = G;
b = B;
rpreview = previewR;
gpreview = previewG;
bpreview = previewB;
rtengine::Color::rgb2hsv01(r, g, b, hue, sat, val);
rtengine::ImProcFunctions::rgb2lab(
static_cast<std::uint8_t>(255 * r),
static_cast<std::uint8_t>(255 * g),
static_cast<std::uint8_t>(255 * b),
L, a, bb,
color_management_params != nullptr
? *color_management_params : App::get().fallbackColorCmp(),
true);
L /= 327.68f;
a /= 327.68f;
bb /= 327.68f;
if (validity != Validity::OUTSIDE) {
setDirty(true);
}
}
void LockableColorPicker::getImagePosition (rtengine::Coord &imgPos) const
{
imgPos = position;
}
void LockableColorPicker::getScreenPosition (rtengine::Coord &screenPos) const
{
if (cropWindow) {
cropWindow->imageCoordToScreen(position.x, position.y, screenPos.x, screenPos.y);
}
}
bool LockableColorPicker::isOver (int x, int y)
{
if (!cropWindow) {
return false;
}
rtengine::Coord pickerScreenPos;
cropWindow->imageCoordToScreen(position.x, position.y, pickerScreenPos.x, pickerScreenPos.y);
rtengine::Coord mousePos(x, y);
rtengine::Coord wh(getWidth(), getHeight());
rtengine::Coord tl(pickerScreenPos - anchorOffset);
rtengine::Coord br(tl + wh);
return mousePos >= tl && mousePos <= br;
}
void LockableColorPicker::setValidity (Validity validity)
{
if (this->validity != validity) {
setDirty(true);
}
this->validity = validity;
}
void LockableColorPicker::setSize (Size newSize)
{
if (size != newSize)
{
size = newSize;
setDirty(true);
}
}
LockableColorPicker::Size LockableColorPicker::getSize () const
{
return size;
}
void LockableColorPicker::rollDisplayedValues ()
{
if (displayedValues < ColorPickerType::LAB) {
displayedValues = (ColorPickerType)(rtengine::toUnderlying(displayedValues) + 1);
} else {
displayedValues = ColorPickerType::RGB;
}
setDirty(true);
}
bool LockableColorPicker::incSize ()
{
if (size < Size::S30) {
size = (Size)(rtengine::toUnderlying(size) + 5);
setDirty(true);
return true;
}
return false;
}
bool LockableColorPicker::decSize ()
{
if (size > Size::S5) {
size = (Size)(rtengine::toUnderlying(size) - 5);
setDirty(true);
return true;
}
return false;
}
// return true if the picker has to be redrawn
bool LockableColorPicker::cycleRGB ()
{
if (displayedValues == ColorPickerType::RGB) {
setDirty (true);
return true;
}
return false;
}
// return true if the picker has to be redrawn
bool LockableColorPicker::cycleHSV ()
{
if (displayedValues == ColorPickerType::HSV) {
setDirty (true);
return true;
}
return false;
}
|