| 1 | // Aseprite Document Library |
| 2 | // Copyright (c) 2019-2022 Igara Studio S.A. |
| 3 | // Copyright (c) 2001-2017 David Capello |
| 4 | // |
| 5 | // This file is released under the terms of the MIT license. |
| 6 | // Read LICENSE.txt for more information. |
| 7 | |
| 8 | #ifndef DOC_PALETTE_PICKS_H_INCLUDED |
| 9 | #define DOC_PALETTE_PICKS_H_INCLUDED |
| 10 | #pragma once |
| 11 | |
| 12 | #include "base/debug.h" |
| 13 | #include "doc/color.h" |
| 14 | |
| 15 | #include <algorithm> |
| 16 | #include <vector> |
| 17 | |
| 18 | namespace doc { |
| 19 | |
| 20 | class PalettePicks { |
| 21 | public: |
| 22 | typedef std::vector<bool> list_type; |
| 23 | typedef list_type::iterator iterator; |
| 24 | typedef list_type::const_iterator const_iterator; |
| 25 | typedef list_type::reference reference; |
| 26 | typedef list_type::const_reference const_reference; |
| 27 | |
| 28 | PalettePicks() { } |
| 29 | PalettePicks(int n) : m_items(n, false) { } |
| 30 | |
| 31 | int size() const { return int(m_items.size()); } |
| 32 | int picks() const { return (int)std::count(m_items.begin(), m_items.end(), true); } |
| 33 | |
| 34 | iterator begin() { return m_items.begin(); } |
| 35 | iterator end() { return m_items.end(); } |
| 36 | |
| 37 | const_iterator begin() const { return m_items.begin(); } |
| 38 | const_iterator end() const { return m_items.end(); } |
| 39 | |
| 40 | const_reference operator[](int idx) const { |
| 41 | ASSERT(idx >= 0 && idx < int(m_items.size())); |
| 42 | return m_items[idx]; |
| 43 | } |
| 44 | |
| 45 | reference operator[](int idx) { |
| 46 | ASSERT(idx >= 0 && idx < int(m_items.size())); |
| 47 | return m_items[idx]; |
| 48 | } |
| 49 | |
| 50 | void resize(int n) { |
| 51 | m_items.resize(n, false); |
| 52 | } |
| 53 | |
| 54 | void clear() { |
| 55 | std::fill(m_items.begin(), m_items.end(), false); |
| 56 | } |
| 57 | |
| 58 | void all() { |
| 59 | std::fill(m_items.begin(), m_items.end(), true); |
| 60 | } |
| 61 | |
| 62 | // If there is just one selected color (or none), we select them all. |
| 63 | void pickAllIfNeeded() { |
| 64 | if (picks() < 2) |
| 65 | all(); |
| 66 | } |
| 67 | |
| 68 | int firstPick() const { |
| 69 | for (int i=0; i<size(); ++i) |
| 70 | if (m_items[i]) |
| 71 | return i; |
| 72 | return -1; |
| 73 | } |
| 74 | |
| 75 | int lastPick() const { |
| 76 | for (int i=size()-1; i>=0; --i) |
| 77 | if (m_items[i]) |
| 78 | return i; |
| 79 | return -1; |
| 80 | } |
| 81 | |
| 82 | std::vector<color_t> toVectorOfIndexes() const { |
| 83 | std::vector<color_t> result(picks()); |
| 84 | for (color_t i=0, j=0; i<(color_t)size(); ++i) { |
| 85 | if (m_items[i]) |
| 86 | result[j++] = i; |
| 87 | } |
| 88 | return result; |
| 89 | } |
| 90 | |
| 91 | void invert() { |
| 92 | for (int i=0; i<size(); ++i) |
| 93 | m_items[i] = !m_items[i]; |
| 94 | } |
| 95 | |
| 96 | private: |
| 97 | list_type m_items; |
| 98 | }; |
| 99 | |
| 100 | } // namespace doc |
| 101 | |
| 102 | #endif |
| 103 | |