Frequency.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 Frequency {
  11. public:
  12. enum class Type {
  13. Hz,
  14. kHz
  15. };
  16. static Optional<Type> unit_from_name(StringView);
  17. Frequency(double value, Type type);
  18. static Frequency make_hertz(double);
  19. Frequency percentage_of(Percentage const&) const;
  20. String to_string() const;
  21. double to_hertz() const;
  22. Type type() const { return m_type; }
  23. double raw_value() const { return m_value; }
  24. bool operator==(Frequency const& other) const
  25. {
  26. return m_type == other.m_type && m_value == other.m_value;
  27. }
  28. int operator<=>(Frequency const& other) const
  29. {
  30. auto this_hertz = to_hertz();
  31. auto other_hertz = other.to_hertz();
  32. if (this_hertz < other_hertz)
  33. return -1;
  34. if (this_hertz > other_hertz)
  35. return 1;
  36. return 0;
  37. }
  38. private:
  39. StringView unit_name() const;
  40. Type m_type;
  41. double m_value { 0 };
  42. };
  43. }
  44. template<>
  45. struct AK::Formatter<Web::CSS::Frequency> : Formatter<StringView> {
  46. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Frequency const& frequency)
  47. {
  48. return Formatter<StringView>::format(builder, frequency.to_string());
  49. }
  50. };