Frequency.h 1.4 KB

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