Skip to content

Commit bae2163

Browse files
committed
Resolve issues
Signed-off-by: Spencer Magnusson <spencer.magnusson@dreamworks.com>
1 parent ffb9b6f commit bae2163

5 files changed

Lines changed: 96 additions & 93 deletions

File tree

src/opentimelineio/clip.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ Clip::color() const noexcept
224224
void
225225
Clip::set_color(Color* color)
226226
{
227-
_color = color ? color : new Color();
227+
_color = color;
228228
}
229229

230230
}} // namespace opentimelineio::OPENTIMELINEIO_VERSION

src/opentimelineio/color.cpp

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,94 @@ const Color Color::purple(0.5, 0.0, 0.5, 1.0, "Purple");
3838
const Color Color::magenta(1.0, 0.0, 1.0, 1.0, "Magenta");
3939
const Color Color::black(0.0, 0.0, 0.0, 1.0, "Black");
4040
const Color Color::white(1.0, 1.0, 1.0, 1.0, "White");
41-
const Color Color::transparent(0.0, 0.0, 0.0, 0.0, "Transparent");
41+
42+
Color*
43+
Color::from_hex(std::string const& color)
44+
{
45+
std::string temp = color;
46+
if (temp[0] == '#')
47+
{
48+
temp = temp.substr(1);
49+
}
50+
else if (temp[0] == '0' and (temp[1] == 'x' or temp[1] == 'X'))
51+
{
52+
temp = temp.substr(2);
53+
}
54+
55+
double _r, _g, _b, _a;
56+
// 0xFFFFFFFF (rgba, 255)
57+
int BASE_16 = 16;
58+
double BASE_16_DIV = 255.0;
59+
double BASE_8_DIV = 15.0;
60+
if (temp.length() == 8)
61+
{
62+
_r = std::stoi(temp.substr(0, 2), nullptr, BASE_16) / BASE_16_DIV;
63+
_g = std::stoi(temp.substr(2, 2), nullptr, BASE_16) / BASE_16_DIV;
64+
_b = std::stoi(temp.substr(4, 2), nullptr, BASE_16) / BASE_16_DIV;
65+
_a = std::stoi(temp.substr(6, 2), nullptr, BASE_16) / BASE_16_DIV;
66+
}
67+
// 0xFFFFFF (rgb, 255)
68+
else if (temp.length() == 6)
69+
{
70+
_r = std::stoi(temp.substr(0, 2), nullptr, BASE_16) / BASE_16_DIV;
71+
_g = std::stoi(temp.substr(2, 2), nullptr, BASE_16) / BASE_16_DIV;
72+
_b = std::stoi(temp.substr(4, 2), nullptr, BASE_16) / BASE_16_DIV;
73+
_a = 1.0;
74+
}
75+
// 0xFFF (rgb, 16)
76+
else if (temp.length() == 3)
77+
{
78+
_r = std::stoi(temp.substr(0, 1), nullptr, BASE_16) / BASE_8_DIV;
79+
_g = std::stoi(temp.substr(1, 1), nullptr, BASE_16) / BASE_8_DIV;
80+
_b = std::stoi(temp.substr(2, 1), nullptr, BASE_16) / BASE_8_DIV;
81+
_a = 1.0;
82+
}
83+
// 0xFFFF (rgba, 16)
84+
else if (temp.length() == 4)
85+
{
86+
_r = std::stoi(temp.substr(0, 1), nullptr, BASE_16) / BASE_8_DIV;
87+
_g = std::stoi(temp.substr(1, 1), nullptr, BASE_16) / BASE_8_DIV;
88+
_b = std::stoi(temp.substr(2, 1), nullptr, BASE_16) / BASE_8_DIV;
89+
_a = std::stoi(temp.substr(3, 1), nullptr, BASE_16) / BASE_8_DIV;
90+
}
91+
else {
92+
throw std::invalid_argument("Invalid hex format");
93+
}
94+
return new Color(_r, _g, _b, _a);
95+
}
96+
97+
Color*
98+
Color::from_int_list(std::vector<int> const& color, int bit_depth)
99+
{
100+
double depth = pow(2, bit_depth) - 1.0; // e.g. 8 = 255.0
101+
if (color.size() == 3)
102+
return new Color(color[0] / depth, color[1] / depth, color[2] / depth, 1.0);
103+
else if (color.size() == 4)
104+
return new Color(color[0] / depth, color[1] / depth, color[2] / depth, color[3] / depth);
105+
106+
throw std::invalid_argument("List must have exactly 3 or 4 elements");
107+
}
108+
109+
Color*
110+
Color::from_agbr_int(unsigned int agbr) noexcept
111+
{
112+
auto conv_r = (agbr & 0xFF) / 255.0;
113+
auto conv_g = ((agbr >> 16) & 0xFF) / 255.0;
114+
auto conv_b = ((agbr >> 8) & 0xFF) / 255.0;
115+
auto conv_a = ((agbr >> 24) & 0xFF) / 255.0;
116+
return new Color(conv_r, conv_g, conv_b, conv_a);
117+
}
118+
119+
Color*
120+
Color::from_float_list(std::vector<double> const& color)
121+
{
122+
if (color.size() == 3)
123+
return new Color(color[0], color[1], color[2], 1.0);
124+
else if (color.size() == 4)
125+
return new Color(color[0], color[1], color[2], color[3]);
126+
127+
throw std::invalid_argument("List must have exactly 3 or 4 elements");
128+
}
42129

43130
std::string
44131
Color::to_hex()

src/opentimelineio/color.h

Lines changed: 4 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -44,95 +44,11 @@ class Color: public SerializableObjectWithMetadata
4444
static const Color magenta;
4545
static const Color black;
4646
static const Color white;
47-
static const Color transparent;
4847

49-
static Color*
50-
from_hex(std::string const& color) noexcept
51-
{
52-
std::string temp = color;
53-
if (temp[0] == '#')
54-
{
55-
temp = temp.substr(1);
56-
}
57-
else if (temp[0] == '0' and (temp[1] == 'x' or temp[1] == 'X'))
58-
{
59-
temp = temp.substr(2);
60-
}
61-
62-
double _r, _g, _b, _a;
63-
// 0xFFFFFFFF (rgba, 255)
64-
int BASE_16 = 16;
65-
double BASE_16_DIV = 255.0;
66-
double BASE_8_DIV = 15.0;
67-
if (temp.length() == 8)
68-
{
69-
_r = std::stoi(temp.substr(0, 2), nullptr, BASE_16) / BASE_16_DIV;
70-
_g = std::stoi(temp.substr(2, 2), nullptr, BASE_16) / BASE_16_DIV;
71-
_b = std::stoi(temp.substr(4, 2), nullptr, BASE_16) / BASE_16_DIV;
72-
_a = std::stoi(temp.substr(6, 2), nullptr, BASE_16) / BASE_16_DIV;
73-
}
74-
// 0xFFFFFF (rgb, 255)
75-
else if (temp.length() == 6)
76-
{
77-
_r = std::stoi(temp.substr(0, 2), nullptr, BASE_16) / BASE_16_DIV;
78-
_g = std::stoi(temp.substr(2, 2), nullptr, BASE_16) / BASE_16_DIV;
79-
_b = std::stoi(temp.substr(4, 2), nullptr, BASE_16) / BASE_16_DIV;
80-
_a = 1.0;
81-
}
82-
// 0xFFF (rgb, 16)
83-
else if (temp.length() == 3)
84-
{
85-
_r = std::stoi(temp.substr(0, 1), nullptr, BASE_16) / BASE_8_DIV;
86-
_g = std::stoi(temp.substr(1, 1), nullptr, BASE_16) / BASE_8_DIV;
87-
_b = std::stoi(temp.substr(2, 1), nullptr, BASE_16) / BASE_8_DIV;
88-
_a = 1.0;
89-
}
90-
// 0xFFFF (rgba, 16)
91-
else if (temp.length() == 4)
92-
{
93-
_r = std::stoi(temp.substr(0, 1), nullptr, BASE_16) / BASE_8_DIV;
94-
_g = std::stoi(temp.substr(1, 1), nullptr, BASE_16) / BASE_8_DIV;
95-
_b = std::stoi(temp.substr(2, 1), nullptr, BASE_16) / BASE_8_DIV;
96-
_a = std::stoi(temp.substr(3, 1), nullptr, BASE_16) / BASE_8_DIV;
97-
}
98-
else {
99-
throw std::invalid_argument("Invalid hex format");
100-
}
101-
return new Color(_r, _g, _b, _a);
102-
}
103-
104-
static Color*
105-
from_int_list(std::vector<int> const& color, int bit_depth) noexcept
106-
{
107-
double depth = pow(2, bit_depth) - 1.0; // e.g. 8 = 255.0
108-
if (color.size() == 3)
109-
return new Color(color[0] / depth, color[1] / depth, color[2] / depth, 1.0);
110-
else if (color.size() == 4)
111-
return new Color(color[0] / depth, color[1] / depth, color[2] / depth, color[3] / depth);
112-
113-
throw std::invalid_argument("List must have exactly 3 or 4 elements");
114-
}
115-
116-
static Color*
117-
from_agbr_int(unsigned int agbr) noexcept
118-
{
119-
auto conv_r = (agbr & 0xFF) / 255.0;
120-
auto conv_g = ((agbr >> 16) & 0xFF) / 255.0;
121-
auto conv_b = ((agbr >> 8) & 0xFF) / 255.0;
122-
auto conv_a = ((agbr >> 24) & 0xFF) / 255.0;
123-
return new Color(conv_r, conv_g, conv_b, conv_a);
124-
}
125-
126-
static Color*
127-
from_float_list(std::vector<double> const& color) noexcept
128-
{
129-
if (color.size() == 3)
130-
return new Color(color[0], color[1], color[2], 1.0);
131-
else if (color.size() == 4)
132-
return new Color(color[0], color[1], color[2], color[3]);
133-
134-
throw std::invalid_argument("List must have exactly 3 or 4 elements");
135-
}
48+
static Color* from_hex(std::string const& color);
49+
static Color* from_int_list(std::vector<int> const& color, int bit_depth);
50+
static Color* from_agbr_int(unsigned int agbr) noexcept;
51+
static Color* from_float_list(std::vector<double> const& color);
13652

13753
friend bool
13854
operator==(Color lhs, Color rhs) noexcept

src/opentimelineio/track.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Track::write_to(Writer& writer) const
4343
{
4444
Parent::write_to(writer);
4545
writer.write("kind", _kind);
46+
writer.write("color", _color);
4647
}
4748

4849
TimeRange
@@ -318,7 +319,7 @@ Track::color() const noexcept
318319
void
319320
Track::set_color(Color* color)
320321
{
321-
_color = color ? color : new Color();
322+
_color = color;
322323
}
323324

324325
}} // namespace opentimelineio::OPENTIMELINEIO_VERSION

src/py-opentimelineio/opentimelineio-bindings/otio_serializableObjects.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,7 @@ static void define_bases2(py::module m) {
236236
.def_property_readonly_static("PURPLE", [](py::object) { return new Color(Color::purple); }, py::return_value_policy::take_ownership)
237237
.def_property_readonly_static("MAGENTA", [](py::object) { return new Color(Color::magenta); }, py::return_value_policy::take_ownership)
238238
.def_property_readonly_static("BLACK", [](py::object) { return new Color(Color::black); }, py::return_value_policy::take_ownership)
239-
.def_property_readonly_static("WHITE", [](py::object) { return new Color(Color::white); }, py::return_value_policy::take_ownership)
240-
.def_property_readonly_static("TRANSPARENT", [](py::object) { return new Color(Color::transparent); }, py::return_value_policy::take_ownership);
239+
.def_property_readonly_static("WHITE", [](py::object) { return new Color(Color::white); }, py::return_value_policy::take_ownership);
241240

242241
auto marker_class =
243242
py::class_<Marker, SOWithMetadata, managing_ptr<Marker>>(m, "Marker", py::dynamic_attr(), R"docstring(

0 commit comments

Comments
 (0)