Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
mitkColorProperty.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef mitkColorProperty_h
14 #define mitkColorProperty_h
15 
16 #include <mitkBaseProperty.h>
17 #include <MitkCoreExports.h>
18 
19 #include <itkRGBPixel.h>
20 
21 #include <nlohmann/json.hpp>
22 
23 namespace mitk
24 {
30  typedef itk::RGBPixel<float> Color;
31 
40  inline Color MakeColor(float r, float g, float b)
41  {
42  Color color;
43  color.Set(r, g, b);
44  return color;
45  }
46 
62  {
63  protected:
65 
67 
69 
70  ColorProperty(const float red, const float green, const float blue);
71 
72  ColorProperty(const float color[3]);
73 
74  ColorProperty(const mitk::Color &color);
75 
77 
78  public:
80  itkFactorylessNewMacro(Self);
81  itkCloneMacro(Self) mitkNewMacro1Param(ColorProperty, const float *);
83  mitkNewMacro3Param(ColorProperty, const float, const float, const float);
84 
87 
92  const mitk::Color &GetColor() const;
93 
98  const mitk::Color &GetValue() const;
99 
104  std::string GetValueAsString() const override;
105 
113  void SetColor(const mitk::Color &color);
114 
119  void SetValue(const mitk::Color &color);
120 
128  void SetColor(float red, float green, float blue);
129 
136  bool ToJSON(nlohmann::json &j) const override;
137 
144  bool FromJSON(const nlohmann::json &j) override;
145 
146  using BaseProperty::operator=;
147 
148  private:
149  // purposely not implemented
150  ColorProperty &operator=(const ColorProperty &);
151 
152  bool IsEqual(const BaseProperty &property) const override;
153  bool Assign(const BaseProperty &property) override;
154  };
155 
156 
157 } // namespace mitk
158 
159 namespace itk
160 {
161  template <typename TComponent>
162  void to_json(nlohmann::json& j, const RGBPixel<TComponent>& c)
163  {
164  j = nlohmann::json::array();
165 
166  for (size_t i = 0; i < 3; ++i)
167  j.push_back(c[i]);
168  }
169 
170  template <typename TComponent>
171  void from_json(const nlohmann::json& j, RGBPixel<TComponent>& c)
172  {
173  for (size_t i = 0; i < 3; ++i)
174  j.at(i).get_to(c[i]);
175  }
176 } // namespace itk
177 
178 #endif
#define MITKCORE_EXPORT
Abstract base class for properties.
Property for storing RGB color values.
const mitk::Color & GetColor() const
Get the color value.
mitkNewMacro3Param(ColorProperty, const float, const float, const float)
ColorProperty(const float color[3])
void SetColor(float red, float green, float blue)
Set the color from individual RGB components.
std::string GetValueAsString() const override
Return the color as a human-readable string.
mitkCloneMacro(ColorProperty)
void SetValue(const mitk::Color &color)
Set the color value (alias for SetColor()).
ColorProperty(const ColorProperty &other)
mitk::Color ValueType
The type of the value stored by this property.
mitkClassMacro(ColorProperty, BaseProperty)
bool FromJSON(const nlohmann::json &j) override
Deserialize the color from a JSON three-element array [r, g, b].
bool ToJSON(nlohmann::json &j) const override
Serialize the color to JSON as a three-element array [r, g, b].
void SetColor(const mitk::Color &color)
Set the color value.
ColorProperty(const float red, const float green, const float blue)
ColorProperty(const mitk::Color &color)
mitkNewMacro1Param(ColorProperty, const mitk::Color &)
const mitk::Color & GetValue() const
Get the color value (alias for GetColor()).
#define mitkNewMacro1Param(classname, type)
Definition: mitkCommon.h:62
nlohmann::json json
void from_json(const nlohmann::json &j, RGBPixel< TComponent > &c)
void to_json(nlohmann::json &j, const RGBPixel< TComponent > &c)
Find image slices visible on a given plane.
itk::RGBPixel< float > Color
Standard RGB color typedef using float components.
Color MakeColor(float r, float g, float b)
Convenience function to create a Color from individual RGB components.