Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:59:34

0001 //----------------------------------*-C++-*----------------------------------//
0002 // Copyright 2024 UT-Battelle, LLC, and other Celeritas developers.
0003 // See the top-level COPYRIGHT file for details.
0004 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0005 //---------------------------------------------------------------------------//
0006 //! \file geocel/rasterize/Color.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include <cstdint>
0011 #include <string>
0012 #include <string_view>
0013 
0014 #include "corecel/Assert.hh"
0015 #include "corecel/cont/Array.hh"
0016 
0017 namespace celeritas
0018 {
0019 //---------------------------------------------------------------------------//
0020 /*!
0021  * Stora an RGBA color.
0022  *
0023  * This is used to define volume/material colors used for raytrace rendering.
0024  * The "byte" is the bit depth of a channel.
0025  */
0026 class Color
0027 {
0028   public:
0029     //!@{
0030     //! \name Type aliases
0031     using byte_type = std::uint8_t;
0032     using size_type = std::uint32_t;
0033     using Byte4 = Array<byte_type, 4>;
0034     //!@}
0035 
0036     //! Little-endian indexing for RGBA
0037     enum class Channel
0038     {
0039         alpha,
0040         blue,
0041         green,
0042         red,
0043         size_
0044     };
0045 
0046   public:
0047     // Construct with an \c #0178ef RGB string (100% opaque)
0048     static Color from_html(std::string_view rgb);
0049 
0050     // Construct from an RGB hex value
0051     static Color from_rgb(size_type rgb);
0052 
0053     // Construct from an RGBA hex value
0054     static Color from_rgba(size_type rgba);
0055 
0056     // Construct with transparent black
0057     Color() = default;
0058 
0059     //! Construct from an integer representation
0060     explicit CELER_CONSTEXPR_FUNCTION Color(size_type c) : color_{c} {}
0061 
0062     //! Get an integer representation of the color
0063     explicit CELER_CONSTEXPR_FUNCTION operator size_type() const
0064     {
0065         return color_;
0066     }
0067 
0068     // Get representation as HTML color \#RRGGBB
0069     std::string to_html() const;
0070 
0071     // Get a single channel
0072     inline CELER_FUNCTION byte_type channel(Channel c) const;
0073 
0074   private:
0075     //! Encoded color value
0076     size_type color_{0};
0077 };
0078 
0079 //---------------------------------------------------------------------------//
0080 // INLINE DEFINITIONS
0081 //---------------------------------------------------------------------------//
0082 /*!
0083  * Get a single channel.
0084  */
0085 CELER_FUNCTION auto Color::channel(Channel c) const -> byte_type
0086 {
0087     CELER_EXPECT(c < Channel::size_);
0088     size_type result = color_;
0089     result >>= (8 * static_cast<size_type>(c));
0090     result &= 0xffu;
0091     return static_cast<byte_type>(result);
0092 }
0093 
0094 //---------------------------------------------------------------------------//
0095 }  // namespace celeritas