Comment tweaks.
[libdcp.git] / src / types.cc
1 #include <vector>
2 #include <cstdio>
3 #include <iomanip>
4 #include <boost/lexical_cast.hpp>
5 #include <boost/algorithm/string.hpp>
6 #include "types.h"
7 #include "exceptions.h"
8
9 using namespace std;
10 using namespace libdcp;
11 using namespace boost;
12
13 Fraction::Fraction (string s)
14 {
15         vector<string> b;
16         split (b, s, is_any_of (" "));
17         if (b.size() != 2) {
18                 throw XMLError ("malformed fraction " + s + " in XML node");
19         }
20         numerator = lexical_cast<int> (b[0]);
21         denominator = lexical_cast<int> (b[1]);
22 }
23
24 bool
25 libdcp::operator== (Fraction const & a, Fraction const & b)
26 {
27         return (a.numerator == b.numerator && a.denominator == b.denominator);
28 }
29
30 bool
31 libdcp::operator!= (Fraction const & a, Fraction const & b)
32 {
33         return (a.numerator != b.numerator || a.denominator != b.denominator);
34 }
35
36 Color::Color ()
37         : r (0)
38         , g (0)
39         , b (0)
40 {
41
42 }
43
44 Color::Color (int r_, int g_, int b_)
45         : r (r_)
46         , g (g_)
47         , b (b_)
48 {
49
50 }
51
52 /** Construct a Color from an ARGB hex string; the alpha value is ignored.
53  *  @param argb_hex A string of the form AARRGGBB, where e.g. RR is a two-character
54  *  hex value.
55  */
56 Color::Color (string argb_hex)
57 {
58         int alpha;
59         if (sscanf (argb_hex.c_str(), "%2x%2x%2x%2x", &alpha, &r, &g, &b) < 4) {
60                 throw XMLError ("could not parse colour string");
61         }
62 }
63
64 /** @return An ARGB string of the form AARRGGBB, where e.g. RR is a two-character
65  *  hex value.  The alpha value will always be FF (ie 255; maximum alpha).
66  */
67 string
68 Color::to_argb_string () const
69 {
70         stringstream s;
71         s << "FF";
72         s << hex
73           << setw(2) << setfill('0') << r
74           << setw(2) << setfill('0') << g
75           << setw(2) << setfill('0') << b;
76
77         string t = s.str();
78         to_upper (t);
79         return t;
80 }
81
82 /** operator== for Colors.
83  *  @param a First color to compare.
84  *  @param b Second color to compare.
85  */
86 bool
87 libdcp::operator== (Color const & a, Color const & b)
88 {
89         return (a.r == b.r && a.g == b.g && a.b == b.b);
90 }
91
92 /** operator!= for Colors.
93  *  @param a First color to compare.
94  *  @param b Second color to compare.
95  */
96 bool
97 libdcp::operator!= (Color const & a, Color const & b)
98 {
99         return !(a == b);
100 }
101
102 ostream &
103 libdcp::operator<< (ostream& s, Color const & c)
104 {
105         s << "(" << c.r << ", " << c.g << ", " << c.b << ")";
106         return s;
107 }
108
109 string
110 libdcp::effect_to_string (Effect e)
111 {
112         switch (e) {
113         case NONE:
114                 return "none";
115         case BORDER:
116                 return "border";
117         case SHADOW:
118                 return "shadow";
119         }
120
121         throw MiscError ("unknown effect type");
122 }
123
124 Effect
125 libdcp::string_to_effect (string s)
126 {
127         if (s == "none") {
128                 return NONE;
129         } else if (s == "border") {
130                 return BORDER;
131         } else if (s == "shadow") {
132                 return SHADOW;
133         }
134
135         throw DCPReadError ("unknown subtitle effect type");
136 }
137
138 string
139 libdcp::valign_to_string (VAlign v)
140 {
141         switch (v) {
142         case TOP:
143                 return "top";
144         case CENTER:
145                 return "center";
146         case BOTTOM:
147                 return "bottom";
148         }
149
150         throw MiscError ("unknown valign type");
151 }
152
153 VAlign
154 libdcp::string_to_valign (string s)
155 {
156         if (s == "top") {
157                 return TOP;
158         } else if (s == "center") {
159                 return CENTER;
160         } else if (s == "bottom") {
161                 return BOTTOM;
162         }
163         
164         throw DCPReadError ("unknown subtitle valign type");
165 }
166
167