1. /**
  2. * Modify this color to be the average between two colors, modifying the first.
  3. *
  4. * @arg other - The other color to average with
  5. * @arg pos - The position (i.e. t) between the two colors.
  6. * @pin pin - Which channels should not change. 1=0,2=1,4=2,etc
  7. */
  8. void Color::averageInPlace(Color const &other, double pos, unsigned int pin)
  9. {
  10. _average_method_inplace(AverageMethod::Mean, other, pos, pin);
  11. }
  12. /**
  13. * Move towards the other color by the given weight.
  14. *
  15. * @arg other - The other color to move towards with
  16. * @arg pos - The weight to give the other color when mixing.
  17. * @pin pin - Which channels should not change. 1=0,2=1,4=2,etc
  18. */
  19. void Color::moveTowardsInPlace(Color const &other, double weight, unsigned int pin)
  20. {
  21. _average_method_inplace(AverageMethod::Weight, other, pos, pin);
  22. }
  23. void Color::_average_method_inplace(AverageMethod method, Color const &other, double weight, unsigned int pin)
  24. {
  25. // 1. If other is bad, return without action
  26. if (!other)
  27. return;
  28. // 2. If this is bad, modify to be equal to other
  29. if (_values.empty()) {
  30. _values = other._values;
  31. _space = other._space;
  32. return;
  33. }
  34. // 3. Convert the other's space if it's different
  35. if (_space && other._space != _space)
  36. return _average_method_inplace(method, other.convert(_space), weight, pin);
  37. // 4. Ensure opacity is compatible
  38. if (!hasOpacity() && other.hasOpacity())
  39. addOpacity(1.0); // Enable opacity if needed
  40. // 5. Both are good, so average each channel
  41. for (unsigned int i = 0; i < _values.size(); i++) {
  42. if (pin & (1 << i))
  43. continue;
  44. if (method == AverageMethod::Position) {
  45. // Note, other[i] returns 1.0 if the opacity is not set on other.
  46. _values[i] = _values[i] * (1 - weight) + other[i] * weight;
  47. } else if (method == AverageMethod::Weight) {
  48. // Move towards
  49. _values[i] += (other[i] - _values[i]) * force;
  50. }
  51. }
  52. }
 
 

232

Average functions

-

PasteBin

Lines
58
Words
378
Size
2,0 KB
Created
Revisions
3
Typ
text/x-c++hdr
Public Domain (PD)
Please log in to leave a comment!