| 1 | #include <cbehave/cbehave.h> |
| 2 | |
| 3 | #include <color.h> |
| 4 | |
| 5 | #include <float.h> |
| 6 | |
| 7 | |
| 8 | FEATURE(ColorMult, "Multiply") |
| 9 | SCENARIO("Multiply two colors") |
| 10 | GIVEN("two colors") |
| 11 | color_t c1, c2; |
| 12 | c1.r = c1.g = c1.b = c1.a = 123; |
| 13 | c2.r = c2.g = c2.b = c2.a = 234; |
| 14 | |
| 15 | WHEN("I multiply them") |
| 16 | color_t result = ColorMult(c1, c2); |
| 17 | |
| 18 | THEN("the result should be a multiply blend of the two color's components"); |
| 19 | SHOULD_INT_EQUAL(result.r, c1.r * c2.r / 255); |
| 20 | SHOULD_INT_EQUAL(result.g, c1.g * c2.g / 255); |
| 21 | SHOULD_INT_EQUAL(result.b, c1.b * c2.b / 255); |
| 22 | SHOULD_INT_EQUAL(result.a, c1.a * c2.a / 255); |
| 23 | SCENARIO_END |
| 24 | |
| 25 | SCENARIO("Multiply by white") |
| 26 | GIVEN("a color") |
| 27 | color_t c; |
| 28 | c.r = 123; |
| 29 | c.g = 234; |
| 30 | c.b = 45; |
| 31 | |
| 32 | WHEN("I multiply it by white") |
| 33 | color_t white; |
| 34 | white.r = white.g = white.b = 255; |
| 35 | color_t result = ColorMult(c, white); |
| 36 | |
| 37 | THEN("the result should be the same as the color") |
| 38 | SHOULD_INT_EQUAL(result.r, c.r); |
| 39 | SHOULD_INT_EQUAL(result.g, c.g); |
| 40 | SHOULD_INT_EQUAL(result.b, c.b); |
| 41 | SCENARIO_END |
| 42 | |
| 43 | SCENARIO("Multiply by black") |
| 44 | GIVEN("a color") |
| 45 | color_t c; |
| 46 | c.r = 123; |
| 47 | c.g = 234; |
| 48 | c.b = 45; |
| 49 | |
| 50 | WHEN("I multiply it by black") |
| 51 | color_t black; |
| 52 | black.r = black.g = black.b = 0; |
| 53 | color_t result = ColorMult(c, black); |
| 54 | |
| 55 | THEN("the result should be black") |
| 56 | SHOULD_INT_EQUAL(result.r, black.r); |
| 57 | SHOULD_INT_EQUAL(result.g, black.g); |
| 58 | SHOULD_INT_EQUAL(result.b, black.b); |
| 59 | SCENARIO_END |
| 60 | FEATURE_END |
| 61 | |
| 62 | FEATURE(ColorAlphaBlend, "Alpha blend") |
| 63 | SCENARIO("Alpha blend two colors") |
| 64 | GIVEN("two colors") |
| 65 | color_t c1, c2; |
| 66 | c1.r = c1.g = c1.b = c1.a = 123; |
| 67 | c2.r = c2.g = c2.b = c2.a = 234; |
| 68 | |
| 69 | WHEN("I alpha blend them") |
| 70 | color_t result = ColorAlphaBlend(c1, c2); |
| 71 | |
| 72 | THEN("the result should be an alpha blend of the two color's components") |
| 73 | SHOULD_INT_EQUAL(result.r, ((int)c1.r*(255 - c2.a) + (int)c2.r*c2.a)/255); |
| 74 | SHOULD_INT_EQUAL(result.g, ((int)c1.g*(255 - c2.a) + (int)c2.g*c2.a)/255); |
| 75 | SHOULD_INT_EQUAL(result.b, ((int)c1.r*(255 - c2.a) + (int)c2.b*c2.a)/255); |
| 76 | SHOULD_INT_EQUAL(result.a, 255); |
| 77 | SCENARIO_END |
| 78 | |
| 79 | SCENARIO("Alpha blend with full transparent") |
| 80 | GIVEN("a color") |
| 81 | color_t c; |
| 82 | c.r = 123; |
| 83 | c.g = 234; |
| 84 | c.b = 45; |
| 85 | c.a = 42; |
| 86 | |
| 87 | WHEN("I alpha blend it by transparent") |
| 88 | color_t transparent; |
| 89 | transparent.r = transparent.g = transparent.b = 255; |
| 90 | transparent.a = 0; |
| 91 | color_t result = ColorAlphaBlend(c, transparent); |
| 92 | |
| 93 | THEN("the result should be the same as the color") |
| 94 | SHOULD_INT_EQUAL(result.r, c.r); |
| 95 | SHOULD_INT_EQUAL(result.g, c.g); |
| 96 | SHOULD_INT_EQUAL(result.b, c.b); |
| 97 | SHOULD_INT_EQUAL(result.a, 255); |
| 98 | SCENARIO_END |
| 99 | |
| 100 | SCENARIO("Alpha blend with full opaque black") |
| 101 | GIVEN("a color") |
| 102 | color_t c; |
| 103 | c.r = 123; |
| 104 | c.g = 234; |
| 105 | c.b = 45; |
| 106 | c.a = 194; |
| 107 | |
| 108 | WHEN("I alpha blend it by opaque black") |
| 109 | color_t black; |
| 110 | black.r = black.g = black.b = 0; |
| 111 | black.a = 255; |
| 112 | color_t result = ColorAlphaBlend(c, black); |
| 113 | |
| 114 | THEN("the result should be black") |
| 115 | SHOULD_INT_EQUAL(result.r, black.r); |
| 116 | SHOULD_INT_EQUAL(result.g, black.g); |
| 117 | SHOULD_INT_EQUAL(result.b, black.b); |
| 118 | SHOULD_INT_EQUAL(result.a, black.a); |
| 119 | SCENARIO_END |
| 120 | FEATURE_END |
| 121 | |
| 122 | FEATURE(ColorTint, "Tint") |
| 123 | SCENARIO("Tint to gray (0 saturation)") |
| 124 | GIVEN("a color") |
| 125 | color_t c; |
| 126 | c.r = 123; |
| 127 | c.g = 234; |
| 128 | c.b = 45; |
| 129 | |
| 130 | WHEN("I tint it with 0 saturation") |
| 131 | HSV hsv; |
| 132 | hsv.h = 0.0; |
| 133 | hsv.s = 0.0; |
| 134 | hsv.v = 1.0; |
| 135 | c = ColorTint(c, hsv); |
| 136 | |
| 137 | THEN("the result should have equal RGB components") |
| 138 | SHOULD_INT_EQUAL(c.r, c.g); |
| 139 | SHOULD_INT_EQUAL(c.g, c.b); |
| 140 | SCENARIO_END |
| 141 | |
| 142 | SCENARIO("Tint to white (max value)") |
| 143 | GIVEN("a color") |
| 144 | color_t c; |
| 145 | c.r = 123; |
| 146 | c.g = 234; |
| 147 | c.b = 45; |
| 148 | |
| 149 | WHEN("I tint it with max value and no hue") |
| 150 | HSV hsv; |
| 151 | hsv.h = -1.0; |
| 152 | hsv.s = 1.0; |
| 153 | hsv.v = DBL_MAX; |
| 154 | c = ColorTint(c, hsv); |
| 155 | |
| 156 | THEN("the result should be white") |
| 157 | SHOULD_INT_EQUAL(c.r, 255); |
| 158 | SHOULD_INT_EQUAL(c.g, 255); |
| 159 | SHOULD_INT_EQUAL(c.b, 255); |
| 160 | SCENARIO_END |
| 161 | |
| 162 | SCENARIO("Tint to red") |
| 163 | GIVEN("a color") |
| 164 | color_t c; |
| 165 | c.r = 123; |
| 166 | c.g = 234; |
| 167 | c.b = 45; |
| 168 | |
| 169 | WHEN("I tint it with the red hue") |
| 170 | HSV hsv; |
| 171 | hsv.h = 0.0; |
| 172 | hsv.s = 1.0; |
| 173 | hsv.v = 1.0; |
| 174 | c = ColorTint(c, hsv); |
| 175 | |
| 176 | THEN("the result should be red") |
| 177 | SHOULD_INT_GT(c.r, 0); |
| 178 | SHOULD_INT_EQUAL(c.g, 0); |
| 179 | SHOULD_INT_EQUAL(c.b, 0); |
| 180 | SCENARIO_END |
| 181 | |
| 182 | SCENARIO("Tint with nothing") |
| 183 | GIVEN("a color") |
| 184 | color_t c; |
| 185 | c.r = 123; |
| 186 | c.g = 234; |
| 187 | c.b = 45; |
| 188 | |
| 189 | WHEN("I tint it with null values (-1 hue)") |
| 190 | HSV hsv; |
| 191 | hsv.h = -1.0; |
| 192 | hsv.s = 1.0; |
| 193 | hsv.v = 1.0; |
| 194 | color_t result = ColorTint(c, hsv); |
| 195 | |
| 196 | THEN("the result should be the same as the original color") |
| 197 | SHOULD_MEM_EQUAL(&result, &c, sizeof result); |
| 198 | SCENARIO_END |
| 199 | FEATURE_END |
| 200 | |
| 201 | FEATURE(StrColor, "String conversion") |
| 202 | SCENARIO("Convert from hex") |
| 203 | GIVEN("a hex string") |
| 204 | const char *str = "6495ED"; |
| 205 | |
| 206 | WHEN("I convert it into a color") |
| 207 | color_t result = StrColor(str); |
| 208 | |
| 209 | THEN("the result should be the expected color") |
| 210 | color_t expected = { 100, 149, 237, 255 }; |
| 211 | SHOULD_BE_TRUE(ColorEquals(result, expected)); |
| 212 | SCENARIO_END |
| 213 | |
| 214 | SCENARIO("Convert from hex with alpha") |
| 215 | GIVEN("a hex string") |
| 216 | const char *str = "aabbccdd"; |
| 217 | |
| 218 | WHEN("I convert it into a color") |
| 219 | color_t result = StrColor(str); |
| 220 | |
| 221 | THEN("the result should be the expected color") |
| 222 | color_t expected = { 0xaa, 0xbb, 0xcc, 0xdd }; |
| 223 | SHOULD_BE_TRUE(ColorEquals(result, expected)); |
| 224 | SCENARIO_END |
| 225 | |
| 226 | SCENARIO("Convert invalid string") |
| 227 | GIVEN("an invalid string") |
| 228 | const char *str = "unknown"; |
| 229 | |
| 230 | WHEN("I convert it into a color") |
| 231 | color_t result = StrColor(str); |
| 232 | |
| 233 | THEN("the result should be black") |
| 234 | SHOULD_BE_TRUE(ColorEquals(result, colorBlack)); |
| 235 | SCENARIO_END |
| 236 | FEATURE_END |
| 237 | |
| 238 | FEATURE(ColorStr , "Convert to string") |
| 239 | SCENARIO("Convert RGBA") |
| 240 | GIVEN("a colour") |
| 241 | color_t c = { 0x64, 0x95, 0xed, 0xff }; |
| 242 | |
| 243 | WHEN("I convert it to hex") |
| 244 | char buf[COLOR_STR_BUF]; |
| 245 | ColorStr(buf, c); |
| 246 | |
| 247 | THEN("the result should be the expected hex string") |
| 248 | const char *expected = "6495edff"; |
| 249 | SHOULD_STR_EQUAL(buf, expected); |
| 250 | SCENARIO_END |
| 251 | FEATURE_END |
| 252 | |
| 253 | FEATURE(ColorEquals, "Color equals") |
| 254 | SCENARIO("Same RGBA") |
| 255 | GIVEN("two colors with the same values") |
| 256 | color_t c1, c2; |
| 257 | c1.r = c2.r = 123; |
| 258 | c1.g = c2.g = 45; |
| 259 | c1.b = c2.b = 67; |
| 260 | c1.a = c2.a = 89; |
| 261 | |
| 262 | WHEN("I compare them") |
| 263 | |
| 264 | THEN("the result should be true") |
| 265 | SHOULD_BE_TRUE(ColorEquals(c1, c2)); |
| 266 | SCENARIO_END |
| 267 | |
| 268 | SCENARIO("Different alpha") |
| 269 | GIVEN("two colors with the same values except A") |
| 270 | color_t c1, c2; |
| 271 | c1.r = c2.r = 123; |
| 272 | c1.g = c2.g = 45; |
| 273 | c1.b = c2.b = 67; |
| 274 | c1.a = 12; |
| 275 | c2.a = 34; |
| 276 | |
| 277 | WHEN("I compare them") |
| 278 | |
| 279 | THEN("the result should be false") |
| 280 | SHOULD_BE_FALSE(ColorEquals(c1, c2)); |
| 281 | SCENARIO_END |
| 282 | FEATURE_END |
| 283 | |
| 284 | CBEHAVE_RUN( |
| 285 | "Color features are:", |
| 286 | TEST_FEATURE(ColorMult), |
| 287 | TEST_FEATURE(ColorAlphaBlend), |
| 288 | TEST_FEATURE(ColorTint), |
| 289 | TEST_FEATURE(StrColor), |
| 290 | TEST_FEATURE(ColorStr), |
| 291 | TEST_FEATURE(ColorEquals) |
| 292 | ) |