Make 'Color' constants 'constexpr' and add tests

This commit is contained in:
Vittorio Romeo 2022-01-27 02:28:19 +00:00
parent 28f273b9c9
commit 0e419543f2
5 changed files with 29 additions and 56 deletions

View File

@ -80,15 +80,15 @@ public:
////////////////////////////////////////////////////////////
// Static member data
////////////////////////////////////////////////////////////
SFML_GRAPHICS_API static const Color Black; //!< Black predefined color
SFML_GRAPHICS_API static const Color White; //!< White predefined color
SFML_GRAPHICS_API static const Color Red; //!< Red predefined color
SFML_GRAPHICS_API static const Color Green; //!< Green predefined color
SFML_GRAPHICS_API static const Color Blue; //!< Blue predefined color
SFML_GRAPHICS_API static const Color Yellow; //!< Yellow predefined color
SFML_GRAPHICS_API static const Color Magenta; //!< Magenta predefined color
SFML_GRAPHICS_API static const Color Cyan; //!< Cyan predefined color
SFML_GRAPHICS_API static const Color Transparent; //!< Transparent (black) predefined color
static const Color Black; //!< Black predefined color
static const Color White; //!< White predefined color
static const Color Red; //!< Red predefined color
static const Color Green; //!< Green predefined color
static const Color Blue; //!< Blue predefined color
static const Color Yellow; //!< Yellow predefined color
static const Color Magenta; //!< Magenta predefined color
static const Color Cyan; //!< Cyan predefined color
static const Color Transparent; //!< Transparent (black) predefined color
////////////////////////////////////////////////////////////
// Member data

View File

@ -147,3 +147,21 @@ constexpr Color& operator *=(Color& left, const Color& right)
{
return left = left * right;
}
////////////////////////////////////////////////////////////
// Static member data
////////////////////////////////////////////////////////////
// Note: the 'inline' keyword here is technically not required, but VS2019 fails
// to compile with a bogus "multiple definition" error if not explicitly used.
inline constexpr Color Color::Black(0, 0, 0);
inline constexpr Color Color::White(255, 255, 255);
inline constexpr Color Color::Red(255, 0, 0);
inline constexpr Color Color::Green(0, 255, 0);
inline constexpr Color Color::Blue(0, 0, 255);
inline constexpr Color Color::Yellow(255, 255, 0);
inline constexpr Color Color::Magenta(255, 0, 255);
inline constexpr Color Color::Cyan(0, 255, 255);
inline constexpr Color Color::Transparent(0, 0, 0, 0);

View File

@ -6,7 +6,6 @@ set(SRCROOT ${PROJECT_SOURCE_DIR}/src/SFML/Graphics)
set(SRC
${SRCROOT}/BlendMode.cpp
${INCROOT}/BlendMode.hpp
${SRCROOT}/Color.cpp
${INCROOT}/Color.hpp
${INCROOT}/Color.inl
${INCROOT}/Export.hpp

View File

@ -1,46 +0,0 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2022 Laurent Gomila (laurent@sfml-dev.org)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics/Color.hpp>
namespace sf
{
////////////////////////////////////////////////////////////
// Static member data
////////////////////////////////////////////////////////////
const Color Color::Black(0, 0, 0);
const Color Color::White(255, 255, 255);
const Color Color::Red(255, 0, 0);
const Color Color::Green(0, 255, 0);
const Color Color::Blue(0, 0, 255);
const Color Color::Yellow(255, 255, 0);
const Color Color::Magenta(255, 0, 255);
const Color Color::Cyan(0, 255, 255);
const Color Color::Transparent(0, 0, 0, 0);
} // namespace sf

View File

@ -98,5 +98,7 @@ TEST_CASE("sf::Color class - [graphics]")
static_assert(c.a == 4);
static_assert(c + c == sf::Color(2, 4, 6, 8));
static_assert(sf::Color::Black == sf::Color(0, 0, 0, 255));
}
}