changeset 12:0c62e0af6af7

Client: add JavaScript bindings for Color, #445
author David Demelier <markand@malikania.fr>
date Fri, 01 Apr 2016 13:45:45 +0200
parents fe95a8db2970
children fb7c2c096101
files libclient/CMakeLists.txt libclient/malikania/js-color.cpp libclient/malikania/js-color.h tests/libclient/CMakeLists.txt tests/libclient/js-color/CMakeLists.txt tests/libclient/js-color/main.cpp
diffstat 6 files changed, 989 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/libclient/CMakeLists.txt	Fri Apr 01 13:43:30 2016 +0200
+++ b/libclient/CMakeLists.txt	Fri Apr 01 13:45:45 2016 +0200
@@ -24,6 +24,7 @@
 	${CMAKE_CURRENT_SOURCE_DIR}/malikania/color.h
 	${CMAKE_CURRENT_SOURCE_DIR}/malikania/font.h
 	${CMAKE_CURRENT_SOURCE_DIR}/malikania/image.h
+	${CMAKE_CURRENT_SOURCE_DIR}/malikania/js-color.h
 	${CMAKE_CURRENT_SOURCE_DIR}/malikania/label.h
 	${CMAKE_CURRENT_SOURCE_DIR}/malikania/line.h
 	${CMAKE_CURRENT_SOURCE_DIR}/malikania/point.h
@@ -43,6 +44,7 @@
 	${CMAKE_CURRENT_SOURCE_DIR}/malikania/color.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/malikania/font.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/malikania/image.cpp
+	${CMAKE_CURRENT_SOURCE_DIR}/malikania/js-color.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/malikania/label.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/malikania/sprite.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/malikania/window.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libclient/malikania/js-color.cpp	Fri Apr 01 13:45:45 2016 +0200
@@ -0,0 +1,186 @@
+/*
+ * js-color.cpp -- color description (JavaScript binding)
+ *
+ * Copyright (c) 2013-2016 Malikania Authors
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <cassert>
+
+#include "js-color.h"
+#include "util.h"
+
+namespace malikania {
+
+namespace {
+
+std::uint8_t clampComponent(duk::ContextPtr ctx, int value, bool required)
+{
+	if (value < 0 || value > 255) {
+		if (required) {
+			duk::raise(ctx, DUK_ERR_RANGE_ERROR, "%d is out of range (0, 255)", value);
+		} else {
+			value = util::clamp(value, 0, 255);
+		}
+	}
+
+	return static_cast<std::uint8_t>(value);
+}
+
+Color parseString(duk::ContextPtr ctx, duk::Index index, bool required)
+{
+	assert(duk::type(ctx, index) == DUK_TYPE_STRING);
+
+	Color color;
+
+	try {
+		color = Color(duk::get<std::string>(ctx, index));
+	} catch (const std::exception &ex) {
+		if (required) {
+			duk::raise(ctx, DUK_ERR_ERROR, "%s", ex.what());
+		}
+	}
+
+	return color;
+}
+
+Color parseObject(duk::ContextPtr ctx, duk::Index index, bool required)
+{
+	assert(duk::type(ctx, index) == DUK_TYPE_OBJECT);
+
+	auto require = [&] (const std::string &property) -> std::uint8_t {
+		if (required && !duk::hasProperty(ctx, index, property)) {
+			duk::raise(ctx, DUK_ERR_ERROR, "missing %s property in color description", property.c_str());
+		}
+
+		return clampComponent(ctx, duk::getProperty<int>(ctx, index, property), required);
+	};
+
+	return Color(
+		require("red"),
+		require("green"),
+		require("blue"),
+		clampComponent(ctx, duk::optionalProperty<int>(ctx, index, "alpha", 255), required)
+	);
+}
+
+Color parse(duk::ContextPtr ctx, duk::Index index, bool required, Color color = {})
+{
+	switch (duk::type(ctx, index)) {
+	case DUK_TYPE_STRING:
+		color = parseString(ctx, index, required);
+		break;
+	case DUK_TYPE_OBJECT:
+		color = parseObject(ctx, index, required);
+		break;
+	default:
+		if (required) {
+			duk::raise(ctx, DUK_ERR_TYPE_ERROR, "color (string, object) expected");
+		}
+
+		break;
+	}
+
+	return color;
+}
+
+duk::Ret constructor(duk::ContextPtr ctx)
+{
+	Color color;
+
+	/*
+	 * The constructor allows an additional signature that takes 4 number arguments, otherwise use the literal
+	 * parsing functions.
+	 */
+	if (duk::top(ctx) >= 3) {
+		color = Color(
+			clampComponent(ctx, duk::require<int>(ctx, 0), true),
+			clampComponent(ctx, duk::require<int>(ctx, 1), true),
+			clampComponent(ctx, duk::require<int>(ctx, 2), true),
+			clampComponent(ctx, duk::optional<int>(ctx, 3, 255), true)
+		);
+	} else if (duk::top(ctx) == 1) {
+		color = parse(ctx, 0, true);
+	}
+
+	duk::Ret ret;
+
+	/* Allow both constructor and non constructor calls */
+	if (duk_is_constructor_call(ctx)) {
+		duk::push(ctx, duk::This());
+		/* TODO: use duk::put when available */
+		duk::TypeTraits<Color>::put(ctx, color);
+		duk::pop(ctx);
+		ret = 0;
+	} else {
+		duk::push(ctx, color);
+		ret = 1;
+	}
+
+	return ret;
+}
+
+} //! namespace
+
+namespace duk {
+
+Color TypeTraits<Color>::get(ContextPtr ctx, Index index)
+{
+	return parse(ctx, index, false);
+}
+
+Color TypeTraits<Color>::require(ContextPtr ctx, Index index)
+{
+	return parse(ctx, index, true);
+}
+
+Color TypeTraits<Color>::optional(ContextPtr ctx, Index index, Color def)
+{
+	return parse(ctx, index, false, std::move(def));
+}
+
+void TypeTraits<Color>::push(ContextPtr ctx, const Color &color)
+{
+	duk::StackAssert sa(ctx, 1);
+
+	duk::push(ctx, Object());
+	/* TODO: use duk::put when available */
+	duk::TypeTraits<Color>::put(ctx, color);
+
+}
+
+void TypeTraits<Color>::put(ContextPtr ctx, const Color &color)
+{
+	assert(type(ctx, -1) == DUK_TYPE_OBJECT);
+
+	StackAssert sa(ctx, 0);
+
+	putProperty(ctx, -1, "red", static_cast<int>(color.red()));
+	putProperty(ctx, -1, "green", static_cast<int>(color.green()));
+	putProperty(ctx, -1, "blue", static_cast<int>(color.blue()));
+	putProperty(ctx, -1, "alpha", static_cast<int>(color.alpha()));
+}
+
+} // !duk
+
+void loadMalikaniaColor(duk::ContextPtr ctx)
+{
+	duk::StackAssert sa(ctx, 0);
+
+	duk::getGlobal<void>(ctx, "Malikania");
+	duk::putProperty(ctx, -1, "Color", duk::Function{constructor, DUK_VARARGS});
+	duk::pop(ctx);
+}
+
+} // !malikania
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libclient/malikania/js-color.h	Fri Apr 01 13:45:45 2016 +0200
@@ -0,0 +1,102 @@
+/*
+ * js-color.h -- color description (JavaScript binding)
+ *
+ * Copyright (c) 2013-2016 Malikania Authors
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef MALIKANIA_JS_COLOR_H
+#define MALIKANIA_JS_COLOR_H
+
+#include "color.h"
+#include "js.h"
+
+namespace malikania {
+
+namespace duk {
+
+/**
+ * @brief JavaScript binding for Color.
+ *
+ * Colors can be created from plain JavaScript object.
+ *
+ * ````
+ * {
+ *   red: 0,
+ *   green: 255,
+ *   blue: 255,
+ *   alpha: 255
+ * }
+ * ````
+ *
+ * It can also takes strings like "#rrggbbaa" and SVG names.
+ */
+template <>
+class TypeTraits<Color> {
+public:
+	/**
+	 * Get a color.
+	 *
+	 * May return a default value or a color with adjusted components.
+	 *
+	 * @param ctx the context
+	 * @param index the index
+	 */
+	static Color get(ContextPtr ctx, Index index);
+
+	/**
+	 * Require a color.
+	 *
+	 * If the color has any invalid component, raise a JavaScript error.
+	 *
+	 * @param ctx the context
+	 * @param index the index
+	 */
+	static Color require(ContextPtr ctx, Index index);
+
+	/**
+	 * Like get, but return the default value only if the value at the given index is not an object or not a string,
+	 * otherwise, adjust invalid values.
+	 *
+	 * @param ctx the context
+	 * @param index the index
+	 * @param def the default value
+	 */
+	static Color optional(ContextPtr ctx, Index index, Color def);
+
+	/**
+	 * Push the color as object.
+	 *
+	 * @param ctx the context
+	 * @param color the color
+	 */
+	static void push(ContextPtr ctx, const Color &color);
+
+	/**
+	 * Put the color properties into the object at the top of the stack.
+	 *
+	 * @pre the top value must be an object
+	 * @param ctx the context
+	 * @param color the color
+	 */
+	static void put(ContextPtr ctx, const Color &color);
+};
+
+} // !duk
+
+void loadMalikaniaColor(duk::ContextPtr ctx);
+
+} // !malikania
+
+#endif // !MALIKANIA_JS_COLOR_H
\ No newline at end of file
--- a/tests/libclient/CMakeLists.txt	Fri Apr 01 13:43:30 2016 +0200
+++ b/tests/libclient/CMakeLists.txt	Fri Apr 01 13:45:45 2016 +0200
@@ -25,3 +25,6 @@
 add_subdirectory(rectangle)
 add_subdirectory(size)
 add_subdirectory(sprite)
+
+# JavaScript bindings
+add_subdirectory(js-color)
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/libclient/js-color/CMakeLists.txt	Fri Apr 01 13:45:45 2016 +0200
@@ -0,0 +1,23 @@
+#
+# CMakeLists.txt -- CMake build system for malikania
+#
+# Copyright (c) 2013-2016 Malikania Authors
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+#
+
+malikania_create_test(
+	NAME js-color
+	LIBRARIES libclient
+	SOURCES main.cpp
+)
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/libclient/js-color/main.cpp	Fri Apr 01 13:45:45 2016 +0200
@@ -0,0 +1,673 @@
+/*
+ * main.cpp -- test Color (JavaScript binding)
+ *
+ * Copyright (c) 2013-2016 Malikania Authors
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <gtest/gtest.h>
+
+#include <malikania/js-color.h>
+
+using namespace malikania;
+
+class TestColor : public testing::Test {
+protected:
+	duk::Context m_ctx;
+
+public:
+	TestColor()
+	{
+		duk::putGlobal(m_ctx, "Malikania", duk::Object());
+
+		loadMalikaniaColor(m_ctx);
+	}
+};
+
+/*
+ * Valid constructors
+ * ------------------------------------------------------------------
+ */
+
+TEST_F(TestColor, ConstructorNoArgs)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"c = Malikania.Color();"
+			"r = c.red;"
+			"g = c.green;"
+			"b = c.blue;"
+			"a = c.alpha;"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "r"));
+		ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "g"));
+		ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "b"));
+		ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "a"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestColor, ConstructorString)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"c = Malikania.Color('white');"
+			"r = c.red;"
+			"g = c.green;"
+			"b = c.blue;"
+			"a = c.alpha;"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "r"));
+		ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "g"));
+		ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "b"));
+		ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "a"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestColor, ConstructorStringRgb)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"c = Malikania.Color('#0000ff');"
+			"r = c.red;"
+			"g = c.green;"
+			"b = c.blue;"
+			"a = c.alpha;"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "r"));
+		ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "g"));
+		ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "b"));
+		ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "a"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestColor, Constructor3Args)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"c = Malikania.Color(10, 20, 30);"
+			"r = c.red;"
+			"g = c.green;"
+			"b = c.blue;"
+			"a = c.alpha;"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(10, duk::getGlobal<int>(m_ctx, "r"));
+		ASSERT_EQ(20, duk::getGlobal<int>(m_ctx, "g"));
+		ASSERT_EQ(30, duk::getGlobal<int>(m_ctx, "b"));
+		ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "a"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestColor, Constructor4Args)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"c = Malikania.Color(10, 20, 30, 40);"
+			"r = c.red;"
+			"g = c.green;"
+			"b = c.blue;"
+			"a = c.alpha;"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(10, duk::getGlobal<int>(m_ctx, "r"));
+		ASSERT_EQ(20, duk::getGlobal<int>(m_ctx, "g"));
+		ASSERT_EQ(30, duk::getGlobal<int>(m_ctx, "b"));
+		ASSERT_EQ(40, duk::getGlobal<int>(m_ctx, "a"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestColor, ConstructorObjectNoAlpha)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"c = Malikania.Color({ red: 10, green: 20, blue: 30 });"
+			"r = c.red;"
+			"g = c.green;"
+			"b = c.blue;"
+			"a = c.alpha;"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(10, duk::getGlobal<int>(m_ctx, "r"));
+		ASSERT_EQ(20, duk::getGlobal<int>(m_ctx, "g"));
+		ASSERT_EQ(30, duk::getGlobal<int>(m_ctx, "b"));
+		ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "a"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestColor, ConstructorObjectAlpha)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"c = Malikania.Color({ red: 10, green: 20, blue: 30, alpha: 40 });"
+			"r = c.red;"
+			"g = c.green;"
+			"b = c.blue;"
+			"a = c.alpha;"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(10, duk::getGlobal<int>(m_ctx, "r"));
+		ASSERT_EQ(20, duk::getGlobal<int>(m_ctx, "g"));
+		ASSERT_EQ(30, duk::getGlobal<int>(m_ctx, "b"));
+		ASSERT_EQ(40, duk::getGlobal<int>(m_ctx, "a"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestColor, ConstructorNew)
+{
+	/*
+	 * Just one test, function parse the same way, only 'this' or a new object is returned.
+	 */
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"c = new Malikania.Color({ red: 10, green: 20, blue: 30, alpha: 40 });"
+			"r = c.red;"
+			"g = c.green;"
+			"b = c.blue;"
+			"a = c.alpha;"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(10, duk::getGlobal<int>(m_ctx, "r"));
+		ASSERT_EQ(20, duk::getGlobal<int>(m_ctx, "g"));
+		ASSERT_EQ(30, duk::getGlobal<int>(m_ctx, "b"));
+		ASSERT_EQ(40, duk::getGlobal<int>(m_ctx, "a"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+/*
+ * Valid constructors
+ * ------------------------------------------------------------------
+ */
+
+TEST_F(TestColor, InvalidConstructorArg1)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"try {"
+			"  Malikania.Color(null);"
+			"} catch (e) {"
+			"  name = e.name;"
+			"  correct = (e instanceof TypeError);"
+			"}"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ("TypeError", duk::getGlobal<std::string>(m_ctx, "name"));
+		ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestColor, InvalidConstructorArg2)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"try {"
+			"  Malikania.Color(10, null, 30);"
+			"} catch (e) {"
+			"  name = e.name;"
+			"  correct = (e instanceof TypeError);"
+			"}"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ("TypeError", duk::getGlobal<std::string>(m_ctx, "name"));
+		ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestColor, InvalidConstructorRange1)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"try {"
+			"  Malikania.Color(-1, 20, 30);"
+			"} catch (e) {"
+			"  name = e.name;"
+			"  correct = (e instanceof RangeError);"
+			"}"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ("RangeError", duk::getGlobal<std::string>(m_ctx, "name"));
+		ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestColor, InvalidConstructorRange2)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"try {"
+			"  Malikania.Color(10, 20, 256);"
+			"} catch (e) {"
+			"  name = e.name;"
+			"  correct = (e instanceof RangeError);"
+			"}"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ("RangeError", duk::getGlobal<std::string>(m_ctx, "name"));
+		ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestColor, InvalidConstructorRange3)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"try {"
+			"  Malikania.Color(10, 20, 30, 800);"
+			"} catch (e) {"
+			"  name = e.name;"
+			"  correct = (e instanceof RangeError);"
+			"}"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ("RangeError", duk::getGlobal<std::string>(m_ctx, "name"));
+		ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestColor, InvalidConstructorRange4)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"try {"
+			"  Malikania.Color({ red: -1, green: 20, blue: 30 });"
+			"} catch (e) {"
+			"  name = e.name;"
+			"  correct = (e instanceof RangeError);"
+			"}"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ("RangeError", duk::getGlobal<std::string>(m_ctx, "name"));
+		ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestColor, InvalidConstructorRange5)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"try {"
+			"  Malikania.Color({ red: 10, green: 20, blue: 30, alpha: 800 });"
+			"} catch (e) {"
+			"  name = e.name;"
+			"  correct = (e instanceof RangeError);"
+			"}"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ("RangeError", duk::getGlobal<std::string>(m_ctx, "name"));
+		ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestColor, InvalidConstructorStringUnknown)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"try {"
+			"  Malikania.Color('does not exist');"
+			"} catch (e) {"
+			"  name = e.name;"
+			"  correct = (e instanceof Error);"
+			"}"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ("Error", duk::getGlobal<std::string>(m_ctx, "name"));
+		ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestColor, InvalidConstructorStringRgb)
+{
+	try {
+		auto ret = duk::pevalString(m_ctx,
+			"try {"
+			"  Malikania.Color('#ghijkl');"
+			"} catch (e) {"
+			"  name = e.name;"
+			"  correct = (e instanceof Error);"
+			"}"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ("Error", duk::getGlobal<std::string>(m_ctx, "name"));
+		ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+/*
+ * Require.
+ * ------------------------------------------------------------------
+ *
+ * TypeTraits<Color>::require expect to have a valid color, it raises an error for any invalid component. Only alpha
+ * can be ommitted but if it is present and incorrect, an exception is also raised.
+ */
+
+TEST_F(TestColor, requireSuccess)
+{
+	try {
+		duk::putGlobal(m_ctx, "draw", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret {
+			Color color = duk::require<Color>(ctx, 0);
+
+			duk::putGlobal(ctx, "r", static_cast<int>(color.red()));
+			duk::putGlobal(ctx, "g", static_cast<int>(color.green()));
+			duk::putGlobal(ctx, "b", static_cast<int>(color.blue()));
+			duk::putGlobal(ctx, "a", static_cast<int>(color.alpha()));
+
+			return 0;
+		}, 1});
+
+		auto ret = duk::pevalString(m_ctx, "draw('#ff0000');");
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "r"));
+		ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "g"));
+		ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "b"));
+		ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "a"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestColor, requireFail)
+{
+	try {
+		duk::putGlobal(m_ctx, "draw", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret {
+			duk::require<Color>(ctx, 0);
+
+			return 0;
+		}, 1});
+
+		auto ret = duk::pevalString(m_ctx,
+			"try {"
+			"  draw('#ghijkl');"
+			"} catch (e) {"
+			"  name = e.name;"
+			"  correct = (e instanceof Error);"
+			"}"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ("Error", duk::getGlobal<std::string>(m_ctx, "name"));
+		ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestColor, requireFailAlpha)
+{
+	try {
+		duk::putGlobal(m_ctx, "draw", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret {
+			duk::require<Color>(ctx, 0);
+
+			return 0;
+		}, 1});
+
+		auto ret = duk::pevalString(m_ctx,
+			"try {"
+			"  draw({ red: 10, green: 20, blue: 30, alpha: 800 });"
+			"} catch (e) {"
+			"  name = e.name;"
+			"  correct = (e instanceof RangeError);"
+			"}"
+		);
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ("RangeError", duk::getGlobal<std::string>(m_ctx, "name"));
+		ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+/*
+ * Get.
+ * ------------------------------------------------------------------
+ *
+ * TypeTraits<Color>::get readjust any invalid values.
+ */
+
+TEST_F(TestColor, getNormal)
+{
+	try {
+		duk::putGlobal(m_ctx, "draw", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret {
+			Color color = duk::get<Color>(ctx, 0);
+
+			duk::putGlobal(ctx, "r", static_cast<int>(color.red()));
+			duk::putGlobal(ctx, "g", static_cast<int>(color.green()));
+			duk::putGlobal(ctx, "b", static_cast<int>(color.blue()));
+			duk::putGlobal(ctx, "a", static_cast<int>(color.alpha()));
+
+			return 0;
+		}, 1});
+
+		auto ret = duk::pevalString(m_ctx, "draw('#ff0000');");
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "r"));
+		ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "g"));
+		ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "b"));
+		ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "a"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestColor, getAdjustRgb)
+{
+	try {
+		duk::putGlobal(m_ctx, "draw", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret {
+			Color color = duk::get<Color>(ctx, 0);
+
+			duk::putGlobal(ctx, "r", static_cast<int>(color.red()));
+			duk::putGlobal(ctx, "g", static_cast<int>(color.green()));
+			duk::putGlobal(ctx, "b", static_cast<int>(color.blue()));
+			duk::putGlobal(ctx, "a", static_cast<int>(color.alpha()));
+
+			return 0;
+		}, 1});
+
+		auto ret = duk::pevalString(m_ctx, "draw('#ghijkl');");
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "r"));
+		ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "g"));
+		ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "b"));
+		ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "a"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestColor, getAdjustAll)
+{
+	try {
+		duk::putGlobal(m_ctx, "draw", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret {
+			Color color = duk::get<Color>(ctx, 0);
+
+			duk::putGlobal(ctx, "r", static_cast<int>(color.red()));
+			duk::putGlobal(ctx, "g", static_cast<int>(color.green()));
+			duk::putGlobal(ctx, "b", static_cast<int>(color.blue()));
+			duk::putGlobal(ctx, "a", static_cast<int>(color.alpha()));
+
+			return 0;
+		}, 1});
+
+		auto ret = duk::pevalString(m_ctx, "draw({ red: -1, green: 256, blue: 100, alpha: 800 });");
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "r"));
+		ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "g"));
+		ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "b"));
+		ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "a"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+TEST_F(TestColor, getSomethingElse)
+{
+	try {
+		duk::putGlobal(m_ctx, "draw", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret {
+			Color color = duk::get<Color>(ctx, 0);
+
+			duk::putGlobal(ctx, "r", static_cast<int>(color.red()));
+			duk::putGlobal(ctx, "g", static_cast<int>(color.green()));
+			duk::putGlobal(ctx, "b", static_cast<int>(color.blue()));
+			duk::putGlobal(ctx, "a", static_cast<int>(color.alpha()));
+
+			return 0;
+		}, 1});
+
+		auto ret = duk::pevalString(m_ctx, "draw(null);");
+
+		if (ret != 0) {
+			throw duk::error(m_ctx, -1);
+		}
+
+		ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "r"));
+		ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "g"));
+		ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "b"));
+		ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "a"));
+	} catch (const std::exception &ex) {
+		FAIL() << ex.what();
+	}
+}
+
+int main(int argc, char **argv)
+{
+	testing::InitGoogleTest(&argc, argv);
+
+	return RUN_ALL_TESTS();
+}