< Summary

Class:Imagini.Drawing.Palette
Assembly:Imagini.2D
File(s):/home/razer/vscode-projects/project-grove/imagini/Imagini.2D/Drawing/Palette.cs
Covered lines:37
Uncovered lines:1
Coverable lines:38
Total lines:121
Line coverage:97.3% (37 of 38)
Covered branches:11
Total branches:12
Branch coverage:91.6% (11 of 12)

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
.ctor(...)20100%100%
.ctor(...)10100%100%
.ctor(...)10100%100%
Dispose()10100%100%
Destroy()20100%50%
.cctor()10100%100%

File(s)

/home/razer/vscode-projects/project-grove/imagini/Imagini.2D/Drawing/Palette.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Drawing;
 4using System.Linq;
 5using System.Runtime.InteropServices;
 6using static SDL2.SDL_pixels;
 7using static Imagini.ErrorHandler;
 8
 9namespace Imagini.Drawing
 10{
 11    /// <summary>
 12    /// Contains palette information.
 13    /// </summary>
 14    public sealed class Palette : Resource, IDisposable
 15    {
 16        /// <summary>
 17        /// Defines maximum color count in a palette.
 18        /// </summary>
 19        public const int MaximumColors = 256;
 20        internal IntPtr Handle;
 21        private Color[] _colors;
 22        public Color[] Colors
 23        {
 24            get
 25            {
 326                CheckIfNotDisposed();
 327                return _colors;
 28            }
 29            set
 30            {
 531                var count = value.Length;
 532                if (count < 1 || count > MaximumColors)
 233                    throw new ArgumentOutOfRangeException("Invalid color count - maximum of 256 is supported");
 334                var sdlColors = new SDL_Color[count];
 103835                for (int i = 0; i < count; i++)
 51636                    sdlColors[i] = value[i].ToSDLColor();
 37                unsafe
 38                {
 339                    var colorPtr = (SDL_Color*)Marshal.AllocHGlobal(sizeof(SDL_Color) * sdlColors.Length);
 103840                    for (int i = 0; i < count; i++)
 51641                        *(colorPtr + i) = value[i].ToSDLColor();
 042                    Try(() =>
 343                        SDL_SetPaletteColors(Handle, (IntPtr)colorPtr, 0, count),
 344                        "SDL_SetPaletteColors");
 45                }
 346                _colors = value;
 347            }
 48        }
 49
 150        internal Palette(IntPtr handle)
 51        {
 152            Handle = handle;
 153            var palette = Marshal.PtrToStructure<SDL_Palette>(Handle);
 154            var p = palette.colors;
 155            var colors = new Color[palette.ncolors];
 51456            for (int i = 0; i < palette.ncolors; i++)
 57            {
 58                unsafe
 59                {
 25660                    var ptr = (IntPtr)(p + i * 4);
 25661                    var color = Marshal.PtrToStructure<SDL_Color>(ptr);
 25662                    colors[i] = color.FromSDLColor();
 63                }
 64            }
 165            Colors = colors;
 166        }
 67
 68        /// <summary>
 69        /// Creates a new palette from the specified colors.
 70        /// </summary>
 471        public Palette(params Color[] colors)
 72        {
 473            Handle = SDL_AllocPalette(colors.Length);
 474            Colors = colors;
 275        }
 76
 77        /// <summary>
 78        /// Creates a new palette from the specified colors.
 79        /// </summary>
 380        public Palette(IEnumerable<Color> colors) : this(colors.ToArray()) { }
 81
 82        /// <summary>
 83        /// Disposes the object.
 84        /// </summary>
 185        public void Dispose() => Destroy();
 86
 87        internal override void Destroy()
 88        {
 289            if (IsDisposed) return;
 290            base.Destroy();
 291            SDL_FreePalette(Handle);
 292        }
 93
 194        static Palette() => Lifecycle.TryInitialize();
 95    }
 96
 97    internal static class ColorExtensions
 98    {
 99        public static Color FromSDLColor(this SDL_Color color) =>
 100            Color.FromArgb(color.a, color.r, color.g, color.b);
 101
 102        public static SDL_Color ToSDLColor(this Color color) =>
 103            new SDL_Color()
 104            {
 105                a = color.A,
 106                r = color.R,
 107                g = color.G,
 108                b = color.B
 109            };
 110
 111        public static uint AsUint(this Color color, PixelFormatInfo format) =>
 112            SDL_MapRGBA(format.Handle, color.R, color.G, color.B, color.A);
 113
 114        public static Color FromUint(uint value, PixelFormatInfo format)
 115        {
 116            byte r = 0; byte g = 0; byte b = 0; byte a = 0;
 117            SDL_GetRGBA(value, format.Handle, ref r, ref g, ref b, ref a);
 118            return Color.FromArgb(a, r, g, b);
 119        }
 120    }
 121}