Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BLI_color_test.cc « tests « blenlib « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 14796e6bf715ec053e98beb57f158e54ac7e1bb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* Apache License, Version 2.0 */

#include "testing/testing.h"

#include "BLI_color.hh"

namespace blender::tests {

/**
 * \name Conversions
 * \{ */

TEST(color, ThemeByteToFloat)
{
  ColorTheme4b theme_byte(192, 128, 64, 128);
  ColorTheme4f theme_float = theme_byte.to_4f();
  EXPECT_NEAR(0.75f, theme_float.r, 0.01f);
  EXPECT_NEAR(0.5f, theme_float.g, 0.01f);
  EXPECT_NEAR(0.25f, theme_float.b, 0.01f);
  EXPECT_NEAR(0.5f, theme_float.a, 0.01f);
}

TEST(color, SrgbStraightFloatToByte)
{
  ColorTheme4f theme_float(0.75f, 0.5f, 0.25f, 0.5f);
  ColorTheme4b theme_byte = theme_float.to_4b();
  EXPECT_EQ(191, theme_byte.r);
  EXPECT_EQ(128, theme_byte.g);
  EXPECT_EQ(64, theme_byte.b);
  EXPECT_EQ(128, theme_byte.a);
}

TEST(color, SrgbStraightToSceneLinearPremultiplied)
{
  BLI_init_srgb_conversion();

  ColorTheme4b theme(192, 128, 64, 128);
  ColorSceneLinear4f<eAlpha::Premultiplied> linear =
      BLI_color_convert_to_scene_linear(theme).premultiply_alpha();
  EXPECT_NEAR(0.26f, linear.r, 0.01f);
  EXPECT_NEAR(0.11f, linear.g, 0.01f);
  EXPECT_NEAR(0.02f, linear.b, 0.01f);
  EXPECT_NEAR(0.5f, linear.a, 0.01f);
}

TEST(color, SceneLinearStraightToPremultiplied)
{
  ColorSceneLinear4f<eAlpha::Straight> straight(0.75f, 0.5f, 0.25f, 0.5f);
  ColorSceneLinear4f<eAlpha::Premultiplied> premultiplied = straight.premultiply_alpha();
  EXPECT_NEAR(0.37f, premultiplied.r, 0.01f);
  EXPECT_NEAR(0.25f, premultiplied.g, 0.01f);
  EXPECT_NEAR(0.12f, premultiplied.b, 0.01f);
  EXPECT_NEAR(0.5f, premultiplied.a, 0.01f);
}

TEST(color, SceneLinearPremultipliedToStraight)
{
  ColorSceneLinear4f<eAlpha::Premultiplied> premultiplied(0.75f, 0.5f, 0.25f, 0.5f);
  ColorSceneLinear4f<eAlpha::Straight> straight = premultiplied.unpremultiply_alpha();
  EXPECT_NEAR(1.5f, straight.r, 0.01f);
  EXPECT_NEAR(1.0f, straight.g, 0.01f);
  EXPECT_NEAR(0.5f, straight.b, 0.01f);
  EXPECT_NEAR(0.5f, straight.a, 0.01f);
}

TEST(color, SceneLinearStraightSrgbFloat)
{
  BLI_init_srgb_conversion();
  ColorSceneLinear4f<eAlpha::Straight> linear(0.75f, 0.5f, 0.25f, 0.5f);
  ColorTheme4f theme = BLI_color_convert_to_theme4f(linear);
  EXPECT_NEAR(0.88f, theme.r, 0.01);
  EXPECT_NEAR(0.73f, theme.g, 0.01);
  EXPECT_NEAR(0.53f, theme.b, 0.01);
  EXPECT_NEAR(0.5f, theme.a, 0.01);
}

TEST(color, SceneLinearPremultipliedToSrgbFloat)
{
  BLI_init_srgb_conversion();
  ColorSceneLinear4f<eAlpha::Premultiplied> linear(0.75f, 0.5f, 0.25f, 0.5f);
  ColorTheme4f theme = BLI_color_convert_to_theme4f(linear.unpremultiply_alpha());

  EXPECT_NEAR(1.19f, theme.r, 0.01);
  EXPECT_NEAR(1.0f, theme.g, 0.01);
  EXPECT_NEAR(0.74f, theme.b, 0.01);
  EXPECT_NEAR(0.5f, theme.a, 0.01);
}

TEST(color, SceneLinearStraightSrgbByte)
{
  BLI_init_srgb_conversion();
  ColorSceneLinear4f<eAlpha::Straight> linear(0.75f, 0.5f, 0.25f, 0.5f);
  ColorTheme4b theme = BLI_color_convert_to_theme4b(linear);
  EXPECT_EQ(225, theme.r);
  EXPECT_EQ(188, theme.g);
  EXPECT_EQ(137, theme.b);
  EXPECT_EQ(128, theme.a);
}

TEST(color, SceneLinearPremultipliedToSrgbByte)
{
  BLI_init_srgb_conversion();
  ColorSceneLinear4f<eAlpha::Premultiplied> linear(0.75f, 0.5f, 0.25f, 0.5f);
  ColorTheme4b theme = BLI_color_convert_to_theme4b(linear.unpremultiply_alpha());
  EXPECT_EQ(255, theme.r);
  EXPECT_EQ(255, theme.g);
  EXPECT_EQ(188, theme.b);
  EXPECT_EQ(128, theme.a);
}

TEST(color, SceneLinearByteEncoding)
{
  ColorSceneLinear4f<eAlpha::Premultiplied> linear(0.75f, 0.5f, 0.25f, 0.5f);
  ColorSceneLinearByteEncoded4b<eAlpha::Premultiplied> encoded = linear.encode();
  EXPECT_EQ(225, encoded.r);
  EXPECT_EQ(188, encoded.g);
  EXPECT_EQ(137, encoded.b);
  EXPECT_EQ(128, encoded.a);
}

TEST(color, SceneLinearByteDecoding)
{
  ColorSceneLinearByteEncoded4b<eAlpha::Premultiplied> encoded(225, 188, 137, 128);
  ColorSceneLinear4f<eAlpha::Premultiplied> decoded = encoded.decode();
  EXPECT_NEAR(0.75f, decoded.r, 0.01f);
  EXPECT_NEAR(0.5f, decoded.g, 0.01f);
  EXPECT_NEAR(0.25f, decoded.b, 0.01f);
  EXPECT_NEAR(0.5f, decoded.a, 0.01f);
}

/* \} */

}  // namespace blender::tests