| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ColorFaker.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using MyNet.Generator; |
| | | 8 | | |
| | | 9 | | namespace MyNet.Fakers.Media; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Current implementation of <see cref="IColorFaker"/>. |
| | | 13 | | /// </summary> |
| | | 14 | | public sealed class ColorFaker(IRandomGenerator random) : IColorFaker |
| | | 15 | | { |
| | 3 | 16 | | private static readonly string[] NamedColors = |
| | 3 | 17 | | [ |
| | 3 | 18 | | "red", |
| | 3 | 19 | | "blue", |
| | 3 | 20 | | "green", |
| | 3 | 21 | | "yellow", |
| | 3 | 22 | | "purple", |
| | 3 | 23 | | "orange", |
| | 3 | 24 | | "pink", |
| | 3 | 25 | | "black", |
| | 3 | 26 | | "white", |
| | 3 | 27 | | "gray", |
| | 3 | 28 | | "cyan", |
| | 3 | 29 | | "magenta", |
| | 3 | 30 | | "lime", |
| | 3 | 31 | | "teal", |
| | 3 | 32 | | "navy", |
| | 3 | 33 | | "maroon" |
| | 3 | 34 | | ]; |
| | | 35 | | |
| | | 36 | | /// <inheritdoc /> |
| | | 37 | | public string Hex() |
| | | 38 | | { |
| | 3 | 39 | | var r = random.Byte(); |
| | 3 | 40 | | var g = random.Byte(); |
| | 3 | 41 | | var b = random.Byte(); |
| | | 42 | | |
| | 3 | 43 | | return $"#{r:X2}{g:X2}{b:X2}"; |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <inheritdoc /> |
| | 0 | 47 | | public (byte R, byte G, byte B) Rgb() => (random.Byte(), random.Byte(), random.Byte()); |
| | | 48 | | |
| | | 49 | | /// <inheritdoc /> |
| | 3 | 50 | | public (byte A, byte R, byte G, byte B) Argb() => (random.Byte(), random.Byte(), random.Byte(), random.Byte()); |
| | | 51 | | |
| | | 52 | | /// <inheritdoc /> |
| | | 53 | | public string CssRgb() |
| | | 54 | | { |
| | 0 | 55 | | var (r, g, b) = Rgb(); |
| | 0 | 56 | | return $"rgb({r}, {g}, {b})"; |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <inheritdoc /> |
| | | 60 | | public string CssRgba() |
| | | 61 | | { |
| | 3 | 62 | | var (a, r, g, b) = Argb(); |
| | 3 | 63 | | return $"rgba({r}, {g}, {b}, {a / 255.0:F2})"; |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <inheritdoc /> |
| | 3 | 67 | | public string Name() => random.Item(NamedColors); |
| | | 68 | | } |
| | | 69 | | |