Ratio.h 990 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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(double first, double second = 1);
  13. double numerator() const { return m_first_value; }
  14. double denominator() const { return m_second_value; }
  15. double value() const { return m_first_value / m_second_value; }
  16. bool is_degenerate() const;
  17. String to_string() const;
  18. bool operator==(Ratio const& other) const
  19. {
  20. return value() == other.value();
  21. }
  22. int operator<=>(Ratio const& other) const
  23. {
  24. auto this_value = value();
  25. auto other_value = other.value();
  26. if (this_value < other_value)
  27. return -1;
  28. if (this_value > other_value)
  29. return 1;
  30. return 0;
  31. }
  32. private:
  33. double m_first_value { 0 };
  34. double m_second_value { 1 };
  35. };
  36. }