Ratio.h 881 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. namespace Web::CSS {
  9. // https://www.w3.org/TR/css-values-4/#ratios
  10. class Ratio {
  11. public:
  12. Ratio(float first, float second = 1);
  13. float value() const { return m_first_value / m_second_value; }
  14. bool is_degenerate() const;
  15. ErrorOr<String> to_string() const;
  16. bool operator==(Ratio const& other) const
  17. {
  18. return value() == other.value();
  19. }
  20. int operator<=>(Ratio const& other) const
  21. {
  22. auto this_value = value();
  23. auto other_value = other.value();
  24. if (this_value < other_value)
  25. return -1;
  26. if (this_value > other_value)
  27. return 1;
  28. return 0;
  29. }
  30. private:
  31. float m_first_value { 0 };
  32. float m_second_value { 1 };
  33. };
  34. }