Program Listing for File palette.cpp

Return to documentation for file (src/core/palette.cpp)

#include "palette.h"

namespace PixelMaestro {
    Palette::Palette(const Colors::RGB colors[], uint8_t size) {
        set_colors(colors, size);
    }

    Palette::Palette(const Palette &other) {
        set_colors(other.get_colors(), other.get_num_colors());
    }

    Palette& Palette::operator=(const Palette& other) {
        set_colors(other.get_colors(), other.get_num_colors());
        return *this;
    }

    Colors::RGB& Palette::get_color_at_index(uint8_t index) const {
        if (index >= num_colors_) {
            return colors_[index % num_colors_];
        }
        else {
            return colors_[index];
        }
    }

    Colors::RGB* Palette::get_colors() const {
        return colors_;
    }

    uint8_t Palette::get_num_colors() const {
        return num_colors_;
    }

    void Palette::set_colors(const Colors::RGB colors[], uint8_t num_colors) {
        delete [] this->colors_;

        this->num_colors_ = num_colors;
        this->colors_ = new Colors::RGB[num_colors];
        for (uint8_t i = 0; i < num_colors; i++) {
            this->colors_[i] = colors[i];
        }
    }

    Palette::~Palette() {
        delete [] colors_;
    }
}