Frequency.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/RefPtr.h>
  8. #include <LibWeb/Forward.h>
  9. namespace Web::CSS {
  10. class Frequency {
  11. public:
  12. enum class Type {
  13. Calculated,
  14. Hz,
  15. kHz
  16. };
  17. static Optional<Type> unit_from_name(StringView);
  18. Frequency(int value, Type type);
  19. Frequency(float value, Type type);
  20. static Frequency make_calculated(NonnullRefPtr<CalculatedStyleValue>);
  21. static Frequency make_hertz(float);
  22. Frequency percentage_of(Percentage const&) const;
  23. bool is_calculated() const { return m_type == Type::Calculated; }
  24. String to_string() const;
  25. float to_hertz() const;
  26. bool operator==(Frequency const& other) const
  27. {
  28. if (is_calculated())
  29. return m_calculated_style == other.m_calculated_style;
  30. return m_type == other.m_type && m_value == other.m_value;
  31. }
  32. bool operator!=(Frequency const& other) const
  33. {
  34. return !(*this == other);
  35. }
  36. private:
  37. StringView unit_name() const;
  38. Type m_type;
  39. float m_value { 0 };
  40. RefPtr<CalculatedStyleValue> m_calculated_style;
  41. };
  42. }