diff --git a/color.cc b/color.cc index 7d2aea5..b3b52e2 100644 --- a/color.cc +++ b/color.cc @@ -2,3 +2,39 @@ RgbColor::RgbColor(const Color<3>& src) : Color<3>(src) {} + +RgbColor::operator HsvColor() const { + constexpr int32_t kSection = kNumColors / 6; + int32_t max = *std::max_element(this->begin(), this->end()); + int32_t min = *std::min_element(this->begin(), this->end()); + int32_t delta = max - min; + + if (delta == 0) { + return {{{{{0x0000, 0x0000, max}}}}}; + } + + HsvColor ret = {{{{{0, 0, max}}}}}; + + if (max == this->at(0)) { + ret.at(0) = (kSection * (this->at(1) - this->at(2))) / delta + (0 * kSection); + } else if (max == this->at(1)) { + ret.at(0) = (kSection * (this->at(2) - this->at(0))) / delta + (2 * kSection); + } else { + assert(max == this->at(2)); + ret.at(0) = (kSection * (this->at(0) - this->at(1))) / delta + (4 * kSection); + } + + if (ret.at(0) < 0) { + ret.at(0) += kNumColors; + } + + if (max > 0) { + ret.at(1) = static_cast(static_cast(delta) * kMaxColor / max); + } + + return ret; +} + + +HsvColor::HsvColor(const Color<3>& src) + : Color<3>(src) {} diff --git a/color.h b/color.h index c0d874c..0d88099 100644 --- a/color.h +++ b/color.h @@ -9,6 +9,7 @@ constexpr int32_t kMinColor = 0; constexpr int32_t kMaxColor = UINT16_MAX; +constexpr int32_t kNumColors = (kMaxColor - kMinColor) + 1; class ColorBase {}; @@ -20,10 +21,21 @@ struct Color : public Array, public ColorBase { }; +struct RgbColor; +struct HsvColor; + + struct RgbColor : public Color<3> { - public: RgbColor() = default; RgbColor(const Color<3>& src); + + operator HsvColor() const; +}; + + +struct HsvColor : public Color<3> { + HsvColor() = default; + HsvColor(const Color<3>& src); }; @@ -58,7 +70,7 @@ template std::ostream& operator<<(std::ostream& os, const Color& color) { os << std::hex << std::setfill('0') << "Color("; for (int32_t c = 0; c < C; ++c) { - os << "0x" << std::setw(4) << color.at(0); + os << "0x" << std::setw(4) << color.at(c); if (c < C - 1) { os << ", "; } diff --git a/image.h b/image.h index 3e97693..7888a00 100644 --- a/image.h +++ b/image.h @@ -13,8 +13,7 @@ class ImageBase {}; template -class ImageColorBase : public ImageBase { - public: +struct ImageColorBase : public ImageBase { ImageColorBase() = default; ImageColorBase(const ImageColorBase&) = default; virtual ~ImageColorBase() = default;