color_ramp.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*************************************************************************/
  2. /* color_ramp.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef SCENE_RESOURCES_COLOR_RAMP_H_
  31. #define SCENE_RESOURCES_COLOR_RAMP_H_
  32. #include "core/resource.h"
  33. class Gradient : public Resource {
  34. GDCLASS(Gradient, Resource);
  35. OBJ_SAVE_TYPE(Gradient);
  36. public:
  37. struct Point {
  38. float offset;
  39. Color color;
  40. bool operator<(const Point &p_ponit) const {
  41. return offset < p_ponit.offset;
  42. }
  43. };
  44. private:
  45. Vector<Point> points;
  46. bool is_sorted;
  47. protected:
  48. static void _bind_methods();
  49. public:
  50. Gradient();
  51. virtual ~Gradient();
  52. void add_point(float p_offset, const Color &p_color);
  53. void remove_point(int p_index);
  54. void set_points(Vector<Point> &p_points);
  55. Vector<Point> &get_points();
  56. void set_offset(int pos, const float offset);
  57. float get_offset(int pos) const;
  58. void set_color(int pos, const Color &color);
  59. Color get_color(int pos) const;
  60. void set_offsets(const Vector<float> &p_offsets);
  61. Vector<float> get_offsets() const;
  62. void set_colors(const Vector<Color> &p_colors);
  63. Vector<Color> get_colors() const;
  64. _FORCE_INLINE_ Color get_color_at_offset(float p_offset) {
  65. if (points.empty())
  66. return Color(0, 0, 0, 1);
  67. if (!is_sorted) {
  68. points.sort();
  69. is_sorted = true;
  70. }
  71. //binary search
  72. int low = 0;
  73. int high = points.size() - 1;
  74. int middle = 0;
  75. #if DEBUG_ENABLED
  76. if (low > high)
  77. ERR_PRINT("low > high, this may be a bug");
  78. #endif
  79. while (low <= high) {
  80. middle = (low + high) / 2;
  81. const Point &point = points[middle];
  82. if (point.offset > p_offset) {
  83. high = middle - 1; //search low end of array
  84. } else if (point.offset < p_offset) {
  85. low = middle + 1; //search high end of array
  86. } else {
  87. return point.color;
  88. }
  89. }
  90. //return interpolated value
  91. if (points[middle].offset > p_offset) {
  92. middle--;
  93. }
  94. int first = middle;
  95. int second = middle + 1;
  96. if (second >= points.size())
  97. return points[points.size() - 1].color;
  98. if (first < 0)
  99. return points[0].color;
  100. const Point &pointFirst = points[first];
  101. const Point &pointSecond = points[second];
  102. return pointFirst.color.linear_interpolate(pointSecond.color, (p_offset - pointFirst.offset) / (pointSecond.offset - pointFirst.offset));
  103. }
  104. int get_points_count() const;
  105. };
  106. #endif /* SCENE_RESOURCES_COLOR_RAMP_H_ */