diff options
author | Mateusz Piekos <mateuszpiekos@gmail.com> | 2012-06-18 10:55:55 (GMT) |
---|---|---|
committer | Mateusz Piekos <mateuszpiekos@gmail.com> | 2012-06-18 10:55:55 (GMT) |
commit | e0c79b3b885f126a2a2a34cb0d5df90796821130 (patch) | |
tree | d53e7a822a02ebc7ae136d01db18475be4283854 /Swiften/Whiteboard/Elements/Color.cpp | |
parent | f3cc4c80787657ea770468915ec716dea2676d22 (diff) | |
download | swift-contrib-e0c79b3b885f126a2a2a34cb0d5df90796821130.zip swift-contrib-e0c79b3b885f126a2a2a34cb0d5df90796821130.tar.bz2 |
Completed serialization and parsing of line element
Diffstat (limited to 'Swiften/Whiteboard/Elements/Color.cpp')
-rw-r--r-- | Swiften/Whiteboard/Elements/Color.cpp | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/Swiften/Whiteboard/Elements/Color.cpp b/Swiften/Whiteboard/Elements/Color.cpp new file mode 100644 index 0000000..b614a2a --- /dev/null +++ b/Swiften/Whiteboard/Elements/Color.cpp @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2012 Mateusz Piękos + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include <Swiften/Whiteboard/Elements/Color.h> +#include <cstdio> +#include <iomanip> +#include <sstream> +#include <iostream> + +namespace Swift { + Color::Color() : red_(0), green_(0), blue_(0), alpha_(255) { + } + + Color::Color(int red, int green, int blue, int alpha) : red_(red), green_(green), blue_(blue), alpha_(alpha) { + } + + Color::Color(const std::string& hex) : alpha_(255) { + if (hex.size() == 7) { + char temp[3]; + hex.copy(temp, 2, 1); + temp[2] = 0; + std::sscanf(temp, "%x", &red_); + hex.copy(temp, 2, 3); + temp[2] = 0; + std::sscanf(temp, "%x", &green_); + hex.copy(temp, 2, 5); + temp[2] = 0; + std::sscanf(temp, "%x", &blue_); + } + } + + std::string Color::toHex() const { + std::string result = "#"; + std::string hex; + hex = intToStr(red_, 16); + if (hex.size() == 1) { + result += "0"; + } + result += hex; + hex = intToStr(green_, 16); + if (hex.size() == 1) { + result += "0"; + } + result += hex; + hex = intToStr(blue_, 16); + if (hex.size() == 1) { + result += "0"; + } + result += hex; + return result; + } + + int Color::getRed() const { + return red_; + } + + int Color::getGreen() const { + return green_; + } + + int Color::getBlue() const { + return blue_; + } + + int Color::getAlpha() const { + return alpha_; + } + + void Color::setAlpha(int alpha) { + alpha_ = alpha; + } + + std::string Color::intToStr(int t, int base) const { + std::stringstream ss; + ss << std::setbase(base); + ss << t; + return ss.str(); + } +} |