Angle.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/String.h>
  8. #include <LibWeb/Forward.h>
  9. namespace Web::CSS {
  10. class Angle {
  11. public:
  12. enum class Type {
  13. Deg,
  14. Grad,
  15. Rad,
  16. Turn,
  17. };
  18. static Optional<Type> unit_from_name(StringView);
  19. Angle(double value, Type type);
  20. static Angle make_degrees(double);
  21. Angle percentage_of(Percentage const&) const;
  22. String to_string() const;
  23. double to_degrees() const;
  24. double to_radians() const;
  25. Type type() const { return m_type; }
  26. double raw_value() const { return m_value; }
  27. bool operator==(Angle const& other) const
  28. {
  29. return m_type == other.m_type && m_value == other.m_value;
  30. }
  31. int operator<=>(Angle const& other) const
  32. {
  33. auto this_degrees = to_degrees();
  34. auto other_degrees = other.to_degrees();
  35. if (this_degrees < other_degrees)
  36. return -1;
  37. if (this_degrees > other_degrees)
  38. return 1;
  39. return 0;
  40. }
  41. private:
  42. StringView unit_name() const;
  43. Type m_type;
  44. double m_value { 0 };
  45. };
  46. }
  47. template<>
  48. struct AK::Formatter<Web::CSS::Angle> : Formatter<StringView> {
  49. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Angle const& angle)
  50. {
  51. return Formatter<StringView>::format(builder, angle.to_string());
  52. }
  53. };