File: rounded_corners_f.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (85 lines) | stat: -rw-r--r-- 3,038 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_GFX_GEOMETRY_ROUNDED_CORNERS_F_H_
#define UI_GFX_GEOMETRY_ROUNDED_CORNERS_F_H_

#include <iosfwd>
#include <limits>
#include <string>

#include "base/component_export.h"
#include "ui/gfx/geometry/vector2d_f.h"

namespace gfx {

// Represents the geometry of a region with rounded corners, expressed as four
// corner radii in the order: top-left, top-right, bottom-right, bottom-left.
class COMPONENT_EXPORT(GEOMETRY) RoundedCornersF {
 public:
  // Creates an empty RoundedCornersF with all corners having zero radius.
  constexpr RoundedCornersF() : RoundedCornersF(0.0f) {}

  // Creates a RoundedCornersF with the same radius for all corners.
  constexpr explicit RoundedCornersF(float all)
      : RoundedCornersF(all, all, all, all) {}

  // Creates a RoundedCornersF with four different corner radii.
  constexpr RoundedCornersF(float upper_left,
                            float upper_right,
                            float lower_right,
                            float lower_left)
      : upper_left_(clamp(upper_left)),
        upper_right_(clamp(upper_right)),
        lower_right_(clamp(lower_right)),
        lower_left_(clamp(lower_left)) {}

  constexpr float upper_left() const { return upper_left_; }
  constexpr float upper_right() const { return upper_right_; }
  constexpr float lower_right() const { return lower_right_; }
  constexpr float lower_left() const { return lower_left_; }

  void set_upper_left(float upper_left) { upper_left_ = clamp(upper_left); }
  void set_upper_right(float upper_right) { upper_right_ = clamp(upper_right); }
  void set_lower_right(float lower_right) { lower_right_ = clamp(lower_right); }
  void set_lower_left(float lower_left) { lower_left_ = clamp(lower_left); }

  void Set(float upper_left,
           float upper_right,
           float lower_right,
           float lower_left) {
    upper_left_ = clamp(upper_left);
    upper_right_ = clamp(upper_right);
    lower_right_ = clamp(lower_right);
    lower_left_ = clamp(lower_left);
  }

  // Returns true if all of the corners are square (zero effective radius).
  bool IsEmpty() const {
    return upper_left_ == 0.0f && upper_right_ == 0.0f &&
           lower_right_ == 0.0f && lower_left_ == 0.0f;
  }

  friend bool operator==(const RoundedCornersF&,
                         const RoundedCornersF&) = default;

  // Returns a string representation of the insets.
  std::string ToString() const;

 private:
  static constexpr float kTrivial = 8.f * std::numeric_limits<float>::epsilon();

  // Prevents values which are smaller than zero or negligibly small.
  // Uses the same logic as gfx::Size.
  static constexpr float clamp(float f) { return f > kTrivial ? f : 0.f; }

  float upper_left_ = 0.0f;
  float upper_right_ = 0.0f;
  float lower_right_ = 0.0f;
  float lower_left_ = 0.0f;
};

}  // namespace gfx

#endif  // UI_GFX_GEOMETRY_ROUNDED_CORNERS_F_H_