color_ramp.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * color_ramp.h
  3. */
  4. #include "color_ramp.h"
  5. //setter and getter names for property serialization
  6. #define COLOR_RAMP_GET_OFFSETS "get_offsets"
  7. #define COLOR_RAMP_GET_COLORS "get_colors"
  8. #define COLOR_RAMP_SET_OFFSETS "set_offsets"
  9. #define COLOR_RAMP_SET_COLORS "set_colors"
  10. ColorRamp::ColorRamp() {
  11. //Set initial color ramp transition from black to white
  12. points.resize(2);
  13. points[0].color = Color(0,0,0,1);
  14. points[0].offset = 0;
  15. points[1].color = Color(1,1,1,1);
  16. points[1].offset = 1;
  17. is_sorted = true;
  18. }
  19. ColorRamp::~ColorRamp() {
  20. }
  21. void ColorRamp::_bind_methods() {
  22. ObjectTypeDB::bind_method(_MD("add_point","offset","color"),&ColorRamp::add_point);
  23. ObjectTypeDB::bind_method(_MD("remove_point","offset","color"),&ColorRamp::remove_point);
  24. ObjectTypeDB::bind_method(_MD("set_offset","point","offset"),&ColorRamp::set_offset);
  25. ObjectTypeDB::bind_method(_MD("get_offset","point"),&ColorRamp::get_offset);
  26. ObjectTypeDB::bind_method(_MD("set_color","point","color"),&ColorRamp::set_color);
  27. ObjectTypeDB::bind_method(_MD("get_color","point"),&ColorRamp::get_color);
  28. ObjectTypeDB::bind_method(_MD("interpolate","offset"),&ColorRamp::get_color_at_offset);
  29. ObjectTypeDB::bind_method(_MD("get_point_count"),&ColorRamp::get_points_count);
  30. ObjectTypeDB::bind_method(_MD(COLOR_RAMP_SET_OFFSETS,"offsets"),&ColorRamp::set_offsets);
  31. ObjectTypeDB::bind_method(_MD(COLOR_RAMP_GET_OFFSETS),&ColorRamp::get_offsets);
  32. ObjectTypeDB::bind_method(_MD(COLOR_RAMP_SET_COLORS,"colors"),&ColorRamp::set_colors);
  33. ObjectTypeDB::bind_method(_MD(COLOR_RAMP_GET_COLORS),&ColorRamp::get_colors);
  34. ADD_PROPERTY( PropertyInfo(Variant::REAL,"offsets"),_SCS(COLOR_RAMP_SET_OFFSETS),_SCS(COLOR_RAMP_GET_OFFSETS) );
  35. ADD_PROPERTY( PropertyInfo(Variant::REAL,"colors"),_SCS(COLOR_RAMP_SET_COLORS),_SCS(COLOR_RAMP_GET_COLORS) );
  36. }
  37. Vector<float> ColorRamp::get_offsets() const {
  38. Vector<float> offsets;
  39. offsets.resize(points.size());
  40. for(int i = 0; i < points.size(); i++)
  41. {
  42. offsets[i] = points[i].offset;
  43. }
  44. return offsets;
  45. }
  46. Vector<Color> ColorRamp::get_colors() const {
  47. Vector<Color> colors;
  48. colors.resize(points.size());
  49. for(int i = 0; i < points.size(); i++)
  50. {
  51. colors[i] = points[i].color;
  52. }
  53. return colors;
  54. }
  55. void ColorRamp::set_offsets(const Vector<float>& p_offsets) {
  56. points.resize(p_offsets.size());
  57. for(int i = 0; i < points.size(); i++)
  58. {
  59. points[i].offset = p_offsets[i];
  60. }
  61. is_sorted = false;
  62. }
  63. void ColorRamp::set_colors(const Vector<Color>& p_colors) {
  64. if(points.size()<p_colors.size())
  65. is_sorted = false;
  66. points.resize(p_colors.size());
  67. for(int i = 0; i < points.size(); i++)
  68. {
  69. points[i].color = p_colors[i];
  70. }
  71. }
  72. Vector<ColorRamp::Point>& ColorRamp::get_points() {
  73. return points;
  74. }
  75. void ColorRamp::add_point(float p_offset, const Color& p_color) {
  76. Point p;
  77. p.offset=p_offset;
  78. p.color=p_color;
  79. is_sorted=false;
  80. points.push_back(p);
  81. }
  82. void ColorRamp::remove_point(int p_index) {
  83. ERR_FAIL_INDEX(p_index,points.size());
  84. ERR_FAIL_COND(points.size()<=2);
  85. points.remove(p_index);
  86. }
  87. void ColorRamp::set_points(Vector<ColorRamp::Point>& p_points) {
  88. points = p_points;
  89. is_sorted = false;
  90. }
  91. void ColorRamp::set_offset(int pos, const float offset) {
  92. if(points.size() <= pos)
  93. points.resize(pos + 1);
  94. points[pos].offset = offset;
  95. is_sorted = false;
  96. }
  97. float ColorRamp::get_offset(int pos) const {
  98. if(points.size() > pos)
  99. return points[pos].offset;
  100. return 0; //TODO: Maybe throw some error instead?
  101. }
  102. void ColorRamp::set_color(int pos, const Color& color) {
  103. if(points.size() <= pos)
  104. {
  105. points.resize(pos + 1);
  106. is_sorted = false;
  107. }
  108. points[pos].color = color;
  109. }
  110. Color ColorRamp::get_color(int pos) const {
  111. if(points.size() > pos)
  112. return points[pos].color;
  113. return Color(0,0,0,1); //TODO: Maybe throw some error instead?
  114. }
  115. int ColorRamp::get_points_count() const {
  116. return points.size();
  117. }