Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-15 08:59:51

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