PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/base/Drivers/S3Trio64/RGB.cs

#
C# | 97 lines | 74 code | 13 blank | 10 comment | 6 complexity | 60a804ec67e4a9ebf4d1f3737579029a MD5 | raw file
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Microsoft Research Singularity
  4. //
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. //
  7. // File: RGB.cs
  8. //
  9. // Note:
  10. //
  11. using System;
  12. using System.Runtime.CompilerServices;
  13. using Microsoft.SingSharp;
  14. using Microsoft.Singularity;
  15. using Microsoft.Singularity.Channels;
  16. namespace Microsoft.Singularity.Io
  17. {
  18. [CLSCompliant(false)]
  19. public struct RGB
  20. {
  21. byte red;
  22. byte green;
  23. byte blue;
  24. public RGB(byte _red, byte _green, byte _blue)
  25. {
  26. red = _red;
  27. green = _green;
  28. blue = _blue;
  29. }
  30. public RGB(uint color32)
  31. {
  32. red = (byte)((color32 & 0xff0000) >> 16);
  33. green = (byte)((color32 & 0x00ff00) >> 8);
  34. blue = (byte)((color32 & 0x0000ff) >> 0);
  35. }
  36. public static explicit operator uint (RGB color)
  37. {
  38. return Compute32(color.red, color.green, color.blue);
  39. }
  40. public static explicit operator ushort (RGB color)
  41. {
  42. return Compute16(color.red, color.green, color.blue);
  43. }
  44. public static explicit operator byte (RGB color)
  45. {
  46. return Compute4(color.red, color.green, color.blue);
  47. }
  48. public static byte Compute4(byte red, byte green, byte blue)
  49. {
  50. byte c = 0;
  51. if (red > 0x40) {
  52. c |= 0x4;
  53. }
  54. if (green > 0x40) {
  55. c |= 0x2;
  56. }
  57. if (blue > 0x40) {
  58. c |= 0x1;
  59. }
  60. if (red > 0x80 || green > 0x80 || blue > 0x80) {
  61. c |= 0x8;
  62. }
  63. return c;
  64. }
  65. public static ushort Compute16(byte red, byte green, byte blue)
  66. {
  67. return (ushort)((((ushort)red >> 3) << 11) |
  68. (((ushort)green >> 2) << 5) |
  69. ((ushort)blue >> 3));
  70. }
  71. public static uint Compute32(byte red, byte green, byte blue)
  72. {
  73. return ((((uint)red) << 16) |
  74. (((uint)green) << 8) |
  75. ((uint)blue));
  76. }
  77. public static readonly RGB White = new RGB(255, 255, 255);
  78. public static readonly RGB Red = new RGB(255, 0, 0);
  79. public static readonly RGB Green = new RGB(0, 255, 0);
  80. public static readonly RGB Blue = new RGB(0, 0, 255);
  81. public static readonly RGB Black = new RGB(0, 0, 0);
  82. public static readonly RGB Gray = new RGB(127, 127, 127);
  83. };
  84. } // namespace Microsoft.Singularity.Io