style.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2015 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 "../../Core/Text/string_format.h"
32 #include "../../Display/2D/color.h"
33 #include "style_get_value.h"
34 
35 namespace clan
36 {
37  class StyleImpl;
38  class StyleGetValue;
39 
41  class Style
42  {
43  public:
44  Style();
45  Style(const Style &) = delete;
46  ~Style();
47  Style &operator=(const Style &) = delete;
48 
57  void set(const std::string &properties);
58 
59  template <class Arg1, typename... Values>
60  void set(const std::string &properties, Arg1 arg1, Values... values)
61  {
62  set(string_format(properties, arg1, values...));
63  }
64 
66  StyleGetValue declared_value(const char *property_name) const;
67  StyleGetValue declared_value(const std::string &property_name) const { return declared_value(property_name.c_str()); }
68 
70  static std::string to_rgba(const Colorf &c)
71  {
72  return string_format(
73  "rgba(%1,%2,%3,%4)",
74  clamp((int)std::round(c.r * 255), 0, 255),
75  clamp((int)std::round(c.g * 255), 0, 255),
76  clamp((int)std::round(c.b * 255), 0, 255),
77  c.a);
78  }
79 
80  private:
81  std::unique_ptr<StyleImpl> impl;
82  };
83 }
C clamp(A val, B minval, C maxval)
Definition: cl_math.h:98
void set(const std::string &properties)
static std::string to_rgba(const Colorf &c)
Static helper that generates a "rgba(%1,%2,%3,%4)" string for the given color.
Definition: style.h:70
StyleGetValue declared_value(const std::string &property_name) const
Definition: style.h:67
Style property set.
Definition: style.h:42
Floating point color description class (for float).
Definition: color.h:661
StyleGetValue declared_value(const char *property_name) const
Retrieve the declared value for a property.
std::string string_format(const std::string &format)
See clan::StringFormat for details.
Definition: string_format.h:179
Style value returned by style classes.
Definition: style_get_value.h:40
Style & operator=(const Style &)=delete
Definition: clanapp.h:36
void set(const std::string &properties, Arg1 arg1, Values... values)
Definition: style.h:60
Style(const Style &)=delete