rounded_corners_f.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef UI_GFX_GEOMETRY_ROUNDED_CORNERS_F_H_
  5. #define UI_GFX_GEOMETRY_ROUNDED_CORNERS_F_H_
  6. #include <iosfwd>
  7. #include <limits>
  8. #include <string>
  9. #include "ui/gfx/geometry/geometry_export.h"
  10. #include "ui/gfx/geometry/vector2d_f.h"
  11. namespace gfx {
  12. // Represents the geometry of a region with rounded corners, expressed as four
  13. // corner radii in the order: top-left, top-right, bottom-right, bottom-left.
  14. class GEOMETRY_EXPORT RoundedCornersF {
  15. public:
  16. // Creates an empty RoundedCornersF with all corners having zero radius.
  17. constexpr RoundedCornersF() : RoundedCornersF(0.0f) {}
  18. // Creates a RoundedCornersF with the same radius for all corners.
  19. constexpr explicit RoundedCornersF(float all)
  20. : RoundedCornersF(all, all, all, all) {}
  21. // Creates a RoundedCornersF with four different corner radii.
  22. constexpr RoundedCornersF(float upper_left,
  23. float upper_right,
  24. float lower_right,
  25. float lower_left)
  26. : upper_left_(clamp(upper_left)),
  27. upper_right_(clamp(upper_right)),
  28. lower_right_(clamp(lower_right)),
  29. lower_left_(clamp(lower_left)) {}
  30. constexpr float upper_left() const { return upper_left_; }
  31. constexpr float upper_right() const { return upper_right_; }
  32. constexpr float lower_right() const { return lower_right_; }
  33. constexpr float lower_left() const { return lower_left_; }
  34. void set_upper_left(float upper_left) { upper_left_ = clamp(upper_left); }
  35. void set_upper_right(float upper_right) { upper_right_ = clamp(upper_right); }
  36. void set_lower_right(float lower_right) { lower_right_ = clamp(lower_right); }
  37. void set_lower_left(float lower_left) { lower_left_ = clamp(lower_left); }
  38. void Set(float upper_left,
  39. float upper_right,
  40. float lower_right,
  41. float lower_left) {
  42. upper_left_ = clamp(upper_left);
  43. upper_right_ = clamp(upper_right);
  44. lower_right_ = clamp(lower_right);
  45. lower_left_ = clamp(lower_left);
  46. }
  47. // Returns true if all of the corners are square (zero effective radius).
  48. bool IsEmpty() const {
  49. return upper_left_ == 0.0f && upper_right_ == 0.0f &&
  50. lower_right_ == 0.0f && lower_left_ == 0.0f;
  51. }
  52. bool operator==(const RoundedCornersF& corners) const {
  53. return upper_left_ == corners.upper_left_ &&
  54. upper_right_ == corners.upper_right_ &&
  55. lower_right_ == corners.lower_right_ &&
  56. lower_left_ == corners.lower_left_;
  57. }
  58. bool operator!=(const RoundedCornersF& corners) const {
  59. return !(*this == corners);
  60. }
  61. // Returns a string representation of the insets.
  62. std::string ToString() const;
  63. private:
  64. static constexpr float kTrivial = 8.f * std::numeric_limits<float>::epsilon();
  65. // Prevents values which are smaller than zero or negligibly small.
  66. // Uses the same logic as gfx::Size.
  67. static constexpr float clamp(float f) { return f > kTrivial ? f : 0.f; }
  68. float upper_left_ = 0.0f;
  69. float upper_right_ = 0.0f;
  70. float lower_right_ = 0.0f;
  71. float lower_left_ = 0.0f;
  72. };
  73. } // namespace gfx
  74. #endif // UI_GFX_GEOMETRY_ROUNDED_CORNERS_F_H_