LCOV - code coverage report
Current view: top level - xenolith/renderer/material2d/base - MaterialColorScheme.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 13 15 86.7 %
Date: 2024-05-12 00:16:13 Functions: 8 9 88.9 %

          Line data    Source code
       1             : /**
       2             :  Copyright (c) 2023 Stappler LLC <admin@stappler.dev>
       3             : 
       4             :  Permission is hereby granted, free of charge, to any person obtaining a copy
       5             :  of this software and associated documentation files (the "Software"), to deal
       6             :  in the Software without restriction, including without limitation the rights
       7             :  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
       8             :  copies of the Software, and to permit persons to whom the Software is
       9             :  furnished to do so, subject to the following conditions:
      10             : 
      11             :  The above copyright notice and this permission notice shall be included in
      12             :  all copies or substantial portions of the Software.
      13             : 
      14             :  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      15             :  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      16             :  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      17             :  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      18             :  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      19             :  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
      20             :  THE SOFTWARE.
      21             :  **/
      22             : 
      23             : #ifndef XENOLITH_RENDERER_MATERIAL2D_BASE_MATERIALCOLORSCHEME_H_
      24             : #define XENOLITH_RENDERER_MATERIAL2D_BASE_MATERIALCOLORSCHEME_H_
      25             : 
      26             : #include "MaterialConfig.h"
      27             : #include "SPColorHCT.h"
      28             : 
      29             : namespace STAPPLER_VERSIONIZED stappler::xenolith::material2d {
      30             : 
      31             : enum class ThemeType {
      32             :         LightTheme,
      33             :         DarkTheme,
      34             :         Custom
      35             : };
      36             : 
      37             : enum class ColorRole {
      38             :         Primary,
      39             :         OnPrimary,
      40             :         PrimaryContainer,
      41             :         OnPrimaryContainer,
      42             :         Secondary,
      43             :         OnSecondary,
      44             :         SecondaryContainer,
      45             :         OnSecondaryContainer,
      46             :         Tertiary,
      47             :         OnTertiary,
      48             :         TertiaryContainer,
      49             :         OnTertiaryContainer,
      50             :         Error,
      51             :         OnError,
      52             :         ErrorContainer,
      53             :         OnErrorContainer,
      54             :         Background,
      55             :         OnBackground,
      56             :         Surface,
      57             :         OnSurface,
      58             :         SurfaceVariant,
      59             :         OnSurfaceVariant,
      60             :         Outline,
      61             :         OutlineVariant,
      62             :         Shadow,
      63             :         Scrim,
      64             :         InverseSurface,
      65             :         InverseOnSurface,
      66             :         InversePrimary,
      67             :         Undefined,
      68             :         Max
      69             : };
      70             : 
      71             : struct TonalPalette {
      72          12 :         TonalPalette() = default;
      73             : 
      74             :         explicit TonalPalette(const Color4F &color)
      75             :         : TonalPalette(geom::Cam16::create(color)) { }
      76             : 
      77             :         explicit TonalPalette(const geom::Cam16 &cam)
      78             :         : hue(cam.hue), chroma(cam.chroma) { }
      79             : 
      80       18120 :         TonalPalette(geom::Cam16Float hue, geom::Cam16Float chroma)
      81       18120 :         : hue(hue), chroma(chroma) { }
      82             : 
      83       87580 :         Color4F get(geom::Cam16Float tone, float alpha = 1.0f) const {
      84       87580 :                 return ColorHCT::solveColor4F(hue, chroma, tone, alpha);
      85             :         }
      86             : 
      87           0 :         ColorHCT hct(geom::Cam16Float tone, float alpha = 1.0f) const {
      88           0 :                 return ColorHCT(hue, chroma, tone, alpha);
      89             :         }
      90             : 
      91      117822 :         ColorHCT::Values values(geom::Cam16Float tone, float alpha = 1.0f) const {
      92      117822 :                 return ColorHCT::Values{hue, chroma, tone, alpha};
      93             :         }
      94             : 
      95             :         geom::Cam16Float hue = geom::Cam16Float(0.0);
      96             :         geom::Cam16Float chroma = geom::Cam16Float(0.5);
      97             : };
      98             : 
      99             : /**
     100             :  * An intermediate concept between the key color for a UI theme, and a full
     101             :  * color scheme. 5 tonal palettes are generated, all except one use the same
     102             :  * hue as the key color, and all vary in chroma.
     103             :  */
     104             : struct CorePalette {
     105           2 :         CorePalette() = default;
     106             :         CorePalette(const Color4F &, bool isContentColor);
     107             :         CorePalette(const geom::Cam16 &, bool isContentColor);
     108             :         CorePalette(geom::Cam16Float hue, geom::Cam16Float chroma, bool isContentColor);
     109             : 
     110             :         TonalPalette primary;
     111             :         TonalPalette secondary;
     112             :         TonalPalette tertiary;
     113             :         TonalPalette neutral;
     114             :         TonalPalette neutralVariant;
     115             :         TonalPalette error;
     116             : };
     117             : 
     118             : struct ColorScheme {
     119             :         static ColorRole getColorRoleOn(ColorRole, ThemeType);
     120             : 
     121           2 :         ColorScheme() = default;
     122             :         ColorScheme(ThemeType, const CorePalette &);
     123             :         ColorScheme(ThemeType, const Color4F &, bool isContent);
     124             :         ColorScheme(ThemeType, const ColorHCT &, bool isContent);
     125             : 
     126             :         void set(ThemeType, const CorePalette &);
     127             :         void set(ThemeType, const Color4F &, bool isContent);
     128             :         void set(ThemeType, const ColorHCT &, bool isContent);
     129             : 
     130       10422 :         Color4F get(ColorRole name) const {
     131       10422 :                 return colors[toInt(name)];
     132             :         }
     133             : 
     134        8940 :         Color4F on(ColorRole name) const {
     135        8940 :                 return colors[toInt(getColorRoleOn(name, type))];
     136             :         }
     137             : 
     138             :         ColorHCT hct(ColorRole name, float alpha = 1.0f) const;
     139             : 
     140             :         // faster then complete color
     141             :         ColorHCT::Values values(ColorRole name, float alpha = 1.0f) const;
     142             : 
     143             :         ThemeType type = ThemeType::LightTheme;
     144             :         std::array<Color4F, toInt(ColorRole::Max)> colors;
     145             :         CorePalette palette;
     146             : };
     147             : 
     148             : }
     149             : 
     150             : #endif /* XENOLITH_RENDERER_MATERIAL2D_BASE_MATERIALCOLORSCHEME_H_ */

Generated by: LCOV version 1.14