Vuo  2.1.2
VuoRendererColors.cc
Go to the documentation of this file.
1 
10 #include "VuoRendererColors.hh"
11 
12 
13 const qreal VuoRendererColors::minAlpha = 0.35;
14 
15 const int VuoRendererColors::subtleHighlightingLighteningFactor = 140; // 100 means no change. @todo: Re-evaluate for https://b33p.net/kosada/node/6855 .
16 const int VuoRendererColors::activityFadeDuration = 400;
18 bool VuoRendererColors::_isDark = false;
19 
30 VuoRendererColors::VuoRendererColors(VuoNode::TintColor tintColor, VuoRendererColors::SelectionType selectionType, bool isHovered, VuoRendererColors::HighlightType highlightType, qint64 timeOfLastActivity, bool isMissingImplementation)
31 {
32  this->tintColor = tintColor;
33  this->selectionType = selectionType;
34  this->isHovered = isHovered;
35  this->highlightType = highlightType;
36  this->isMissingImplementation = isMissingImplementation;
37 
38  // Turn composition components opaque as they execute or fire events, then fade them back to their minimum transparency.
39  qint64 timeNow = QDateTime::currentMSecsSinceEpoch();
40 
41  this->currentFadePercentage = ((timeOfLastActivity == VuoRendererItem::notTrackingActivity)? 0 :
42  ((timeOfLastActivity == VuoRendererItem::activityInProgress)? 0 :
43  fmin(1, (timeNow - timeOfLastActivity)/(1.0*activityFadeDuration))));
44 }
45 
46 VuoRendererColors *VuoRendererColors::sharedColors = NULL;
47 
53 {
54  if (!sharedColors)
55  sharedColors = new VuoRendererColors();
56 
57  return sharedColors;
58 }
59 
63 void VuoRendererColors::setDark(bool isDark)
64 {
65  _isDark = isDark;
66 }
67 
72 {
73  return _isDark;
74 }
75 
81 {
82  return QColor::fromHslF(0, 0, _isDark ? .19 : 1, 1);
83 }
84 
90 {
91  return applyHighlighting(baseColor());
92 }
93 
98 {
99  return nodeFill();
100 }
101 
106 {
107  return nodeFill();
108 }
109 
114 {
115  if (highlightType == ineligibleHighlight)
116  return Qt::transparent;
117 
118  return _isDark ? nodeClass() : portTitle();
119 }
120 
125 {
126  return portTitle();
127 }
128 
133 {
134  if (isMissingImplementation)
135  return nodeFill();
136  else
137  return applyHighlighting(baseColor().darker(_isDark ? 160 : 130));
138 }
139 
144 {
145  QColor c = applyHighlighting(QColor::fromHslF(0, 0, 1, 1), .2);
146  if (isMissingImplementation)
147  c.setAlphaF(_isDark ? .5 : 1);
148  return c;
149 }
150 
155 {
156  QColor c = applyHighlighting(QColor::fromHslF(0, 0, 1, .6));
157  if (isMissingImplementation)
158  c.setAlphaF(_isDark ? .5 : 1);
159  return c;
160 }
161 
166 {
167  return portFill();
168 }
169 
174 {
175  return portTitle();
176 }
177 
182 {
183  qreal adjustedAlpha = (1. - currentFadePercentage) * getCurrentAlpha();
184  QColor c = portFill();
185  c.setAlphaF(c.alphaF() * adjustedAlpha);
186  return c;
187 }
188 
193 {
194  return nodeFrame();
195 }
196 
201 {
202  return nodeFrame();
203 }
204 
209 {
210  qreal adjustedAlpha = (1. - currentFadePercentage) * getCurrentAlpha();
211  QColor c = nodeFrame();
212  c.setAlphaF(c.alphaF() * adjustedAlpha);
213  return c;
214 }
215 
220 {
221  return nodeFrame();
222 }
223 
228 {
229  if (highlightType == ineligibleHighlight)
230  return Qt::transparent;
231 
232  return applyHighlighting(QColor::fromHslF(0, 0, .2,
233  highlightType == standardHighlight ? 1. : .8),
234  0);
235 }
236 
241 {
242  return actionIndicator();
243 }
244 
249 {
250  return nodeFill();
251 }
252 
257 {
258  return QColor::fromRgb(220, 50, 47, 160);
259 }
260 
265 {
266  QColor darkColor = QColor(0.5*(nodeFrame().red()+nodeFill().red()),
267  0.5*(nodeFrame().green()+nodeFill().green()),
268  0.5*(nodeFrame().blue()+nodeFill().blue()),
269  0.5*(nodeFrame().alpha()+nodeFill().alpha())
270  );
271 
272  QColor frameColor = (_isDark? darkColor : nodeFill());
273  frameColor.setAlpha(_isDark? 0.5*frameColor.alpha() : 0.82*frameColor.alpha());
274 
275  return frameColor;
276 }
277 
282 {
283  QColor c = nodeFill();
284  if (_isDark)
285  {
286  float darkenFactor = .8;
287  QColor b = canvasFill();
288  c.setRedF (c.redF() * (1-darkenFactor) + b.redF() * darkenFactor);
289  c.setGreenF(c.greenF() * (1-darkenFactor) + b.greenF() * darkenFactor);
290  c.setBlueF (c.blueF() * (1-darkenFactor) + b.blueF() * darkenFactor);
291  }
292  else
293  {
294  int lightenFactor = 113;
295  if (tintColor == VuoNode::TintYellow)
296  lightenFactor = 140;
297  else if (tintColor == VuoNode::TintCyan
298  || tintColor == VuoNode::TintGreen
299  || tintColor == VuoNode::TintLime)
300  lightenFactor = 125;
301  c = c.lighter(lightenFactor);
302  }
303 
304  int h,s,v,a;
305  c.getHsv(&h, &s, &v, &a);
306  return (QColor::fromHsv(h, (_isDark? s*4/3 : s/3), v, a)); // Desaturate.
307 }
308 
313 {
314  QColor c = applyHighlighting(QColor::fromHslF(0, 0, 1, .8));
315  return (_isDark? c : portTitle());
316 }
317 
324 {
325  return _isDark ? "#1d6ae5" : "#74acec";
326 }
327 
332 QColor VuoRendererColors::lerpColor(QColor v0, QColor v1, float t)
333 {
334  qreal r0,g0,b0,a;
335  v0.getRgbF(&r0,&g0,&b0,&a);
336 
337  qreal r1,g1,b1;
338  v1.getRgbF(&r1,&g1,&b1);
339 
340  return QColor::fromRgbF(
341  fmin(r0 + (r1 - r0)*t, 1.),
342  fmin(g0 + (g1 - g0)*t, 1.),
343  fmin(b0 + (b1 - b0)*t, 1.),
344  a
345  );
346 }
347 
348 QColor VuoRendererColors::baseColor(bool useTintColor)
349 {
350  if (!useTintColor
351  || tintColor == VuoNode::TintNone ) return QColor::fromHslF( 0./360., 0, getNodeFillLightness(), 1);
352  else if (tintColor == VuoNode::TintYellow ) return QColor::fromHslF( 57./360., 1, .49, 1);
353  else if (tintColor == VuoNode::TintTangerine) return QColor::fromHslF( 41./360., 1, .67, 1);
354  else if (tintColor == VuoNode::TintOrange ) return QColor::fromHslF( 19./360., 1, .73, 1);
355  else if (tintColor == VuoNode::TintMagenta ) return QColor::fromHslF(323./360., 1, .78, 1);
356  else if (tintColor == VuoNode::TintViolet ) return QColor::fromHslF(275./360., 1, .81, 1);
357  else if (tintColor == VuoNode::TintBlue ) return QColor::fromHslF(213./360., 1, .77, 1);
358  else if (tintColor == VuoNode::TintCyan ) return QColor::fromHslF(165./360., 1, .68, 1);
359  else if (tintColor == VuoNode::TintGreen ) return QColor::fromHslF( 99./360., 1, .70, 1);
360  else/*if (tintColor == VuoNode::TintLime )*/return QColor::fromHslF( 70./360., 1, .48, 1);
361 }
362 
363 QColor VuoRendererColors::applyHighlighting(QColor baseColor, qreal selectionIntensity)
364 {
365  qreal alpha = baseColor.alphaF() * getCurrentAlpha();
366  int highlight = 0;
367 
368  if (isHovered)
369  ++highlight;
370 
371  if (selectionType != VuoRendererColors::noSelection)
372  baseColor = lerpColor(baseColor, selection(), selectionIntensity);
373 
374  if (highlightType == VuoRendererColors::standardHighlight)
375  ++highlight;
376  else if (highlightType == VuoRendererColors::subtleHighlight)
377  {
378  if (isHovered)
379  {
380  alpha *= .8;
381  ++highlight;
382  }
383  else
384  alpha *= .6;
385  }
386  else if (highlightType == VuoRendererColors::ineligibleHighlight)
387  alpha *= _isDark ? .1 : .2;
388 
389  if (tintColor != VuoNode::TintNone)
390  highlight *= 2;
391 
392  baseColor = baseColor.lighter(100. + (_isDark ? 10. : -6.) * highlight);
393 
394  baseColor.setAlphaF(alpha);
395  return baseColor;
396 }
397 
404 {
405  return QDateTime::currentMSecsSinceEpoch();
406 }
407 
414 {
415  return QDateTime::currentMSecsSinceEpoch();
416 }
417 
423 {
424  qint64 timeNow = QDateTime::currentMSecsSinceEpoch();
425  qint64 virtualFiredEventOrigin = timeNow - percentage*activityAnimationFadeDuration;
426 
427  return virtualFiredEventOrigin;
428 }
429 
436 {
437  return QDateTime::currentMSecsSinceEpoch();
438 }
439 
445 qreal VuoRendererColors::getCurrentAlpha(void)
446 {
447  return 1 - currentFadePercentage * (1 - minAlpha);
448 }
449 
453 qreal VuoRendererColors::getNodeFillLightness(void)
454 {
455  return _isDark ? .62 : .83;
456 }
457 
463 {
464  // @todo: Account for multiple simultaneous active protocols. https://b33p.net/kosada/node/9585
465  return isInput ? VuoNode::TintBlue : VuoNode::TintLime;
466 }