如何使用模板将数字从0到1标准化?

时间:2021-06-01 04:19:44

I'm designing a class which is RGB color representation. It has three properties: red, green and blue.

我正在设计一个RGB颜色表示的类。它有三个属性:红、绿、蓝。

Until now, I had those 3 properties in type unsigned char int (from 0 to 255). I'd like to use C++ templates, so I could also use other types, like float, double or int.

到目前为止,我已经将这3个属性设置为无符号char类型(从0到255)。我想使用c++模板,所以我也可以使用其他类型,比如float, double或int。

However, I need to know the maximum value, which probably could be solved with std::numeric_limits. The problem are floating point numbers. They should be in range 0..1. Do you have any ideas how to hack this problem?

但是,我需要知道最大值,这可能可以用std::numeric_limit来解决。问题是浮点数。它们应该在0。1的范围内。你知道如何解决这个问题吗?

My goal is to be able convert red, green and blue from not known type into number 0..1.

我的目标是能够将红色、绿色和蓝色从未知类型转换为数字0. 1。

2 个解决方案

#1


2  

template<class T>
RGBClass {
public:
    RGBClass():
        max_value(std::numberic_limits<T>::max())
        /* ... */
    {}
    /* ... */
    const T max_value;

private:
    T R_;
    T G_;
    T B_;
};

template<>
RGBClass<float> {
public: 
    RGBClass():
        max_value(1),
        /* ... */
    {}

    // convert other type to float 0..1
    template<class OTHER_TYPE>
    RGBClass(const OTHER_TYPE& other):
        max_value(1),
        R_((float)other.R_ / (float)other.max_value),
        /* ... */
    {}
    const float max_value;

private:
    float R_;
    float G_;
    float B_;
}

#2


0  

template<typename T, typename=void>
struct pixel_component_range {
  static constexpr T max() { return std::numberic_limits<T>::max(); }
};
template<typename F>
struct pixel_component_range<F, typename std::enable_if<std::is_floating_point<F>::value>::type>
{
  static constexpr T max() { return 1.f; }
};

now pixel_component_range<T>::max() evaluates to the max value of integers, and 1. for floating point values.

现在,pixel_component_range ::max()计算为整数的最大值,以及1。浮点值。

#1


2  

template<class T>
RGBClass {
public:
    RGBClass():
        max_value(std::numberic_limits<T>::max())
        /* ... */
    {}
    /* ... */
    const T max_value;

private:
    T R_;
    T G_;
    T B_;
};

template<>
RGBClass<float> {
public: 
    RGBClass():
        max_value(1),
        /* ... */
    {}

    // convert other type to float 0..1
    template<class OTHER_TYPE>
    RGBClass(const OTHER_TYPE& other):
        max_value(1),
        R_((float)other.R_ / (float)other.max_value),
        /* ... */
    {}
    const float max_value;

private:
    float R_;
    float G_;
    float B_;
}

#2


0  

template<typename T, typename=void>
struct pixel_component_range {
  static constexpr T max() { return std::numberic_limits<T>::max(); }
};
template<typename F>
struct pixel_component_range<F, typename std::enable_if<std::is_floating_point<F>::value>::type>
{
  static constexpr T max() { return 1.f; }
};

now pixel_component_range<T>::max() evaluates to the max value of integers, and 1. for floating point values.

现在,pixel_component_range ::max()计算为整数的最大值,以及1。浮点值。