Palette.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include <LibDraw/Palette.h>
  2. NonnullRefPtr<PaletteImpl> PaletteImpl::create_with_shared_buffer(SharedBuffer& buffer)
  3. {
  4. return adopt(*new PaletteImpl(buffer));
  5. }
  6. PaletteImpl::PaletteImpl(SharedBuffer& buffer)
  7. : m_theme_buffer(buffer)
  8. {
  9. }
  10. Palette::Palette(const PaletteImpl& impl)
  11. : m_impl(impl)
  12. {
  13. }
  14. Palette::~Palette()
  15. {
  16. }
  17. const SystemTheme& PaletteImpl::theme() const
  18. {
  19. return *(const SystemTheme*)m_theme_buffer->data();
  20. }
  21. Color PaletteImpl::color(ColorRole role) const
  22. {
  23. ASSERT((int)role < (int)ColorRole::__Count);
  24. return theme().color[(int)role];
  25. }
  26. NonnullRefPtr<PaletteImpl> PaletteImpl::clone() const
  27. {
  28. auto new_theme_buffer = SharedBuffer::create_with_size(m_theme_buffer->size());
  29. memcpy(new_theme_buffer->data(), m_theme_buffer->data(), m_theme_buffer->size());
  30. return adopt(*new PaletteImpl(*new_theme_buffer));
  31. }
  32. void Palette::set_color(ColorRole role, Color color)
  33. {
  34. if (m_impl->ref_count() != 1)
  35. m_impl = m_impl->clone();
  36. auto& theme = const_cast<SystemTheme&>(impl().theme());
  37. theme.color[(int)role] = color;
  38. }