style_get_value.h
1/*
2** ClanLib SDK
3** Copyright (c) 1997-2020 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
36namespace 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
147 static StyleGetValue from_length(float length, StyleDimension dimension = StyleDimension::px) { StyleGetValue v; v._type = StyleValueType::length; v._value.float_value.number = length; v._value.float_value.dimension = dimension; return v; }
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
159 static StyleGetValue from_resolution(float resolution, StyleDimension dimension = StyleDimension::dppx) { StyleGetValue v; v._type = StyleValueType::resolution; v._value.float_value.number = resolution; v._value.float_value.dimension = dimension; return v; }
160
162 static StyleGetValue from_percentage(float percentage) { StyleGetValue v; v._type = StyleValueType::percentage; v._value.float_value.number = percentage; return v; }
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;
184 float color[4];
185 } _value;
186 };
187}
Floating point color description class (for float).
Definition color.h:799
Style value returned by style classes.
Definition style_get_value.h:40
Colorf color() const
Value color.
Definition style_get_value.h:94
bool is_undefined() const
Check if value is undefined.
Definition style_get_value.h:103
static StyleGetValue from_number(float number)
Create style value from a number.
Definition style_get_value.h:165
static StyleGetValue from_color(const Colorf &color)
Create style value from a color.
Definition style_get_value.h:171
static StyleGetValue from_url(const char *url)
Create style value from an url.
Definition style_get_value.h:168
StyleDimension dimension
Definition style_get_value.h:182
static StyleGetValue from_frequency(float freq, StyleDimension dimension=StyleDimension::hz)
Create style value from a frequency.
Definition style_get_value.h:156
const char * text() const
Text when the type is a text string.
Definition style_get_value.h:46
bool is_url() const
Check if value is an url.
Definition style_get_value.h:135
bool is_keyword(const std::string &keyword) const
Definition style_get_value.h:108
static StyleGetValue from_angle(float angle, StyleDimension dimension=StyleDimension::rad)
Create style value from an angle.
Definition style_get_value.h:150
static StyleGetValue from_resolution(float resolution, StyleDimension dimension=StyleDimension::dppx)
Create style value from a resolution.
Definition style_get_value.h:159
bool is_length() const
Check if value is a length.
Definition style_get_value.h:111
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
static StyleGetValue from_string(const char *text)
Create style value from a string.
Definition style_get_value.h:144
bool is_keyword() const
Check if value is a keyword.
Definition style_get_value.h:106
bool is_resolution() const
Check if value is a resolution.
Definition style_get_value.h:123
StyleDimension dimension() const
Dimension used by value.
Definition style_get_value.h:78
StyleValueType type() const
Variant type.
Definition style_get_value.h:43
static StyleGetValue from_percentage(float percentage)
Create style value from a percentage.
Definition style_get_value.h:162
bool is_color() const
Check if value is a color.
Definition style_get_value.h:138
float number() const
Value number.
Definition style_get_value.h:60
struct clan::StyleGetValue::@204206026116143242266223236344327154320347367141::@107237361046015147027253261123153221146113360330 float_value
static StyleGetValue from_length(float length, StyleDimension dimension=StyleDimension::px)
Create style value from a length.
Definition style_get_value.h:147
bool is_time() const
Check if value is a time.
Definition style_get_value.h:117
static StyleGetValue from_keyword(const char *keyword)
Create style value from a keyword.
Definition style_get_value.h:141
float number
Definition style_get_value.h:181
bool is_percentage() const
Check if value is a percentage.
Definition style_get_value.h:126
bool is_string() const
Check if value is a string.
Definition style_get_value.h:132
float color[4]
Definition style_get_value.h:184
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
bool is_frequency() const
Check if value is a frequency.
Definition style_get_value.h:120
bool is_number() const
Check if value is a number.
Definition style_get_value.h:129
Definition clanapp.h:36
StyleValueType
Style value type.
Definition style_value_type.h:35
@ time
value is an angle
Definition style_value_type.h:45
@ length
value is a keyword
Definition style_value_type.h:38
@ url
value is a text string
Definition style_value_type.h:42
@ undefined
Definition style_value_type.h:36
@ color
value is an url
Definition style_value_type.h:43
@ angle
value is a color
Definition style_value_type.h:44
@ number
value is a percentage number
Definition style_value_type.h:40
@ string
value is a number
Definition style_value_type.h:41
@ resolution
value is a frequency
Definition style_value_type.h:47
@ percentage
value is a length
Definition style_value_type.h:39
@ keyword
value undefined
Definition style_value_type.h:37
@ frequency
value is a time
Definition style_value_type.h:46
@ v
Definition keys.h:102
@ t
Definition keys.h:100
StyleDimension
Unit of a style value.
Definition style_dimension.h:35
@ s
turns (1 in a full circle)
Definition style_dimension.h:54
@ hz
milliseconds
Definition style_dimension.h:56
@ px
Definition style_dimension.h:36
@ rad
gradians/gons/grades (400 in a full circle)
Definition style_dimension.h:52
@ dppx
dots per cm
Definition style_dimension.h:60
@ percentage
Number.
Definition style_token.h:45