From 5f454a856991737da96de872c7fc68589e99e930 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Thu, 17 Jul 2014 19:27:39 +0200 Subject: [PATCH] Made strtorgba into a factory method of RGBAColor The method is RGBAColor::fromString. --- src/gmenu2x.cpp | 3 ++- src/surface.cpp | 18 +++++++++++------- src/surface.h | 3 +-- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/gmenu2x.cpp b/src/gmenu2x.cpp index d0b4de5..436125e 100644 --- a/src/gmenu2x.cpp +++ b/src/gmenu2x.cpp @@ -830,7 +830,8 @@ void GMenu2X::setSkin(const string &skin, bool setWallpaper) { if (value.length()>1 && value.at(0)=='"' && value.at(value.length()-1)=='"') skinConfStr[name] = value.substr(1,value.length()-2); else if (value.at(0) == '#') - skinConfColors[stringToColor(name)] = strtorgba( value.substr(1,value.length()) ); + skinConfColors[stringToColor(name)] = + RGBAColor::fromString(value.substr(1, value.length())); else skinConfInt[name] = atoi(value.c_str()); } diff --git a/src/surface.cpp b/src/surface.cpp index b6d2910..4d98736 100644 --- a/src/surface.cpp +++ b/src/surface.cpp @@ -31,13 +31,17 @@ using namespace std; -RGBAColor strtorgba(const string &strColor) { - RGBAColor c = {0,0,0,255}; - c.r = constrain( strtol( strColor.substr(0,2).c_str(), NULL, 16 ), 0, 255 ); - c.g = constrain( strtol( strColor.substr(2,2).c_str(), NULL, 16 ), 0, 255 ); - c.b = constrain( strtol( strColor.substr(4,2).c_str(), NULL, 16 ), 0, 255 ); - c.a = constrain( strtol( strColor.substr(6,2).c_str(), NULL, 16 ), 0, 255 ); - return c; +RGBAColor RGBAColor::fromString(const string &strColor) { + return { + uint8_t(constrain(strtol(strColor.substr(0, 2).c_str(), nullptr, 16), + 0, 255)), + uint8_t(constrain(strtol(strColor.substr(2, 2).c_str(), nullptr, 16), + 0, 255)), + uint8_t(constrain(strtol(strColor.substr(4, 2).c_str(), nullptr, 16), + 0, 255)), + uint8_t(constrain(strtol(strColor.substr(6, 2).c_str(), nullptr, 16), + 0, 255)), + }; } Surface *Surface::openOutputSurface(int width, int height, int bitsperpixel) { diff --git a/src/surface.h b/src/surface.h index 70981f2..9108b88 100644 --- a/src/surface.h +++ b/src/surface.h @@ -29,10 +29,9 @@ struct RGBAColor { uint8_t r, g, b, a; + static RGBAColor fromString(std::string const& strColor); }; -RGBAColor strtorgba(const std::string &strColor); - /** Wrapper around SDL_Surface @author Massimiliano Torromeo