style_get_value.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2016 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 */
28 
29 #pragma once
30 
31 #include <string>
32 #include "../../Display/2D/color.h"
33 #include "style_value_type.h"
34 #include "style_dimension.h"
35 
36 namespace clan
37 {
40  {
41  public:
43  StyleValueType type() const { return _type; }
44 
46  const char *text() const
47  {
48  switch (_type)
49  {
53  return _value.text;
54  default:
55  return "";
56  }
57  }
58 
60  float number() const
61  {
62  switch (_type)
63  {
71  return _value.float_value.number;
72  default:
73  return 0.0f;
74  }
75  }
76 
79  {
80  switch (_type)
81  {
87  return _value.float_value.dimension;
88  default:
89  return StyleDimension::px;
90  }
91  }
92 
94  Colorf color() const
95  {
96  if (_type == StyleValueType::color)
97  return Colorf(_value.color[0], _value.color[1], _value.color[2], _value.color[3]);
98  else
99  return Colorf();
100  }
101 
103  bool is_undefined() const { return _type == StyleValueType::undefined; }
104 
106  bool is_keyword() const { return _type == StyleValueType::keyword; }
107  bool is_keyword(const char *keyword) const { return is_keyword() && strcmp(_value.text, keyword) == 0; }
108  bool is_keyword(const std::string &keyword) const { return is_keyword() && _value.text == keyword; }
109 
111  bool is_length() const { return _type == StyleValueType::length; }
112 
114  bool is_angle() const { return _type == StyleValueType::angle; }
115 
117  bool is_time() const { return _type == StyleValueType::time; }
118 
120  bool is_frequency() const { return _type == StyleValueType::frequency; }
121 
123  bool is_resolution() const { return _type == StyleValueType::resolution; }
124 
126  bool is_percentage() const { return _type == StyleValueType::percentage; }
127 
129  bool is_number() const { return _type == StyleValueType::number; }
130 
132  bool is_string() const { return _type == StyleValueType::string; }
133 
135  bool is_url() const { return _type == StyleValueType::url; }
136 
138  bool is_color() const { return _type == StyleValueType::color; }
139 
141  static StyleGetValue from_keyword(const char *keyword) { StyleGetValue v; v._type = StyleValueType::keyword; v._value.text = keyword; return v; }
142 
144  static StyleGetValue from_string(const char *text) { StyleGetValue v; v._type = StyleValueType::string; v._value.text = text; return v; }
145 
148 
150  static StyleGetValue from_angle(float angle, StyleDimension dimension = StyleDimension::rad) { StyleGetValue v; v._type = StyleValueType::angle; v._value.float_value.number = angle; v._value.float_value.dimension = dimension; return v; }
151 
153  static StyleGetValue from_time(float t, StyleDimension dimension = StyleDimension::s) { StyleGetValue v; v._type = StyleValueType::time; v._value.float_value.number = t; v._value.float_value.dimension = dimension; return v; }
154 
156  static StyleGetValue from_frequency(float freq, StyleDimension dimension = StyleDimension::hz) { StyleGetValue v; v._type = StyleValueType::frequency; v._value.float_value.number = freq; v._value.float_value.dimension = dimension; return v; }
157 
160 
163 
165  static StyleGetValue from_number(float number) { StyleGetValue v; v._type = StyleValueType::number; v._value.float_value.number = number; return v; }
166 
168  static StyleGetValue from_url(const char *url) { StyleGetValue v; v._type = StyleValueType::url; v._value.text = url; return v; }
169 
171  static StyleGetValue from_color(const Colorf &color) { StyleGetValue v; v._type = StyleValueType::color; v._value.color[0] = color.r; v._value.color[1] = color.g; v._value.color[2] = color.b; v._value.color[3] = color.a; return v; }
172 
173  private:
175 
176  union
177  {
178  const char *text;
179  struct
180  {
181  float number;
183  } float_value;
184  float color[4];
185  } _value;
186  };
187 }
value is an angle
value is a percentage number
static StyleGetValue from_angle(float angle, StyleDimension dimension=StyleDimension::rad)
Create style value from an angle.
Definition: style_get_value.h:150
Definition: clanapp.h:35
Floating point color description class (for float).
Definition: color.h:798
static StyleGetValue from_string(const char *text)
Create style value from a string.
Definition: style_get_value.h:144
float number() const
Value number.
Definition: style_get_value.h:60
turns (1 in a full circle)
Type b
Definition: vec4.h:81
gradians/gons/grades (400 in a full circle)
static StyleGetValue from_color(const Colorf &color)
Create style value from a color.
Definition: style_get_value.h:171
StyleDimension dimension() const
Dimension used by value.
Definition: style_get_value.h:78
static StyleGetValue from_keyword(const char *keyword)
Create style value from a keyword.
Definition: style_get_value.h:141
bool is_color() const
Check if value is a color.
Definition: style_get_value.h:138
StyleDimension dimension
Definition: style_get_value.h:182
bool is_frequency() const
Check if value is a frequency.
Definition: style_get_value.h:120
bool is_percentage() const
Check if value is a percentage.
Definition: style_get_value.h:126
static StyleGetValue from_length(float length, StyleDimension dimension=StyleDimension::px)
Create style value from a length.
Definition: style_get_value.h:147
bool is_undefined() const
Check if value is undefined.
Definition: style_get_value.h:103
bool is_keyword() const
Check if value is a keyword.
Definition: style_get_value.h:106
const char * text() const
Text when the type is a text string.
Definition: style_get_value.h:46
Type r
Definition: vec4.h:79
bool is_string() const
Check if value is a string.
Definition: style_get_value.h:132
float number
Definition: style_get_value.h:181
StyleValueType type() const
Variant type.
Definition: style_get_value.h:43
bool is_number() const
Check if value is a number.
Definition: style_get_value.h:129
static StyleGetValue from_resolution(float resolution, StyleDimension dimension=StyleDimension::dppx)
Create style value from a resolution.
Definition: style_get_value.h:159
Colorf color() const
Value color.
Definition: style_get_value.h:94
Type a
Definition: vec4.h:82
Type g
Definition: vec4.h:80
value is a text string
StyleDimension
Unit of a style value.
Definition: style_dimension.h:34
value is a keyword
float color[4]
Definition: style_get_value.h:184
Style value returned by style classes.
Definition: style_get_value.h:39
static StyleGetValue from_time(float t, StyleDimension dimension=StyleDimension::s)
Create style value from a time.
Definition: style_get_value.h:153
bool is_keyword(const char *keyword) const
Definition: style_get_value.h:107
bool is_url() const
Check if value is an url.
Definition: style_get_value.h:135
StyleValueType
Style value type.
Definition: style_value_type.h:34
const char * text
Definition: style_get_value.h:178
bool is_angle() const
Check if value is an angle.
Definition: style_get_value.h:114
static StyleGetValue from_percentage(float percentage)
Create style value from a percentage.
Definition: style_get_value.h:162
bool is_resolution() const
Check if value is a resolution.
Definition: style_get_value.h:123
static StyleGetValue from_number(float number)
Create style value from a number.
Definition: style_get_value.h:165
bool is_keyword(const std::string &keyword) const
Definition: style_get_value.h:108
bool is_length() const
Check if value is a length.
Definition: style_get_value.h:111
bool is_time() const
Check if value is a time.
Definition: style_get_value.h:117
static StyleGetValue from_frequency(float freq, StyleDimension dimension=StyleDimension::hz)
Create style value from a frequency.
Definition: style_get_value.h:156
struct clan::StyleGetValue::@27::@28 float_value
static StyleGetValue from_url(const char *url)
Create style value from an url.
Definition: style_get_value.h:168