mirror of
https://gitlab.kitware.com/vtk/vtk-m
synced 2024-09-19 10:35:42 +00:00
1541cfa8a6
Adding `0.5` is not correct in all cases. Namely, adding `0.5` will cause the value immediately before it to be rounded to `1` due to the "round to nearest even" rule. Trust the standard library to get this right.
67 lines
1.8 KiB
C++
67 lines
1.8 KiB
C++
//============================================================================
|
|
// Copyright (c) Kitware, Inc.
|
|
// All rights reserved.
|
|
// See LICENSE.txt for details.
|
|
//
|
|
// This software is distributed WITHOUT ANY WARRANTY; without even
|
|
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
// PURPOSE. See the above copyright notice for more information.
|
|
//============================================================================
|
|
#ifndef vtk_m_worklet_colorconversion_Conversions_h
|
|
#define vtk_m_worklet_colorconversion_Conversions_h
|
|
|
|
#include <cmath>
|
|
|
|
namespace vtkm
|
|
{
|
|
namespace worklet
|
|
{
|
|
namespace colorconversion
|
|
{
|
|
|
|
template <typename T>
|
|
VTKM_EXEC inline vtkm::UInt8 ColorToUChar(T t)
|
|
{
|
|
return static_cast<vtkm::UInt8>(t);
|
|
}
|
|
|
|
template <>
|
|
VTKM_EXEC inline vtkm::UInt8 ColorToUChar(vtkm::Float64 t)
|
|
{
|
|
return static_cast<vtkm::UInt8>(std::round(t * 255.0f));
|
|
}
|
|
|
|
template <>
|
|
VTKM_EXEC inline vtkm::UInt8 ColorToUChar(vtkm::Float32 t)
|
|
{
|
|
return static_cast<vtkm::UInt8>(std::round(t * 255.0f));
|
|
}
|
|
|
|
|
|
VTKM_EXEC inline void Clamp(vtkm::Float32& val)
|
|
{
|
|
val = vtkm::Min(255.0f, vtkm::Max(0.0f, val));
|
|
}
|
|
VTKM_EXEC inline void Clamp(vtkm::Vec2f_32& val)
|
|
{
|
|
val[0] = vtkm::Min(255.0f, vtkm::Max(0.0f, val[0]));
|
|
val[1] = vtkm::Min(255.0f, vtkm::Max(0.0f, val[1]));
|
|
}
|
|
VTKM_EXEC inline void Clamp(vtkm::Vec3f_32& val)
|
|
{
|
|
val[0] = vtkm::Min(255.0f, vtkm::Max(0.0f, val[0]));
|
|
val[1] = vtkm::Min(255.0f, vtkm::Max(0.0f, val[1]));
|
|
val[2] = vtkm::Min(255.0f, vtkm::Max(0.0f, val[2]));
|
|
}
|
|
VTKM_EXEC inline void Clamp(vtkm::Vec4f_32& val)
|
|
{
|
|
val[0] = vtkm::Min(255.0f, vtkm::Max(0.0f, val[0]));
|
|
val[1] = vtkm::Min(255.0f, vtkm::Max(0.0f, val[1]));
|
|
val[2] = vtkm::Min(255.0f, vtkm::Max(0.0f, val[2]));
|
|
val[3] = vtkm::Min(255.0f, vtkm::Max(0.0f, val[3]));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endif
|