color.d 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. module color;
  2. import std.stdio;
  3. import std.conv;
  4. import std.math.exponential;
  5. import std.ascii;
  6. import std.algorithm;
  7. import std.format;
  8. struct Color {
  9. ubyte r;
  10. ubyte g;
  11. ubyte b;
  12. private bool isTerminal;
  13. this(ubyte r, ubyte g, ubyte b) {
  14. this.r = r;
  15. this.g = g;
  16. this.b = b;
  17. }
  18. private this(bool isTerminal) {
  19. this.isTerminal = isTerminal;
  20. }
  21. static Color Red = Color(255, 0, 0);
  22. static Color Green = Color(0, 255, 0);
  23. static Color Blue = Color(0, 0, 255);
  24. // Credits go to https://sashamaps.net/docs/resources/20-colors/
  25. static Color Maroon = Color.fromHex("800000");
  26. static Color Brown = Color.fromHex("9a6324");
  27. static Color Olive = Color.fromHex("808000");
  28. static Color Teal = Color.fromHex("469990");
  29. static Color Navy = Color.fromHex("000075");
  30. static Color Black = Color.fromHex("000000");
  31. static Color Orange = Color.fromHex("f58231");
  32. static Color Yellow = Color.fromHex("ffe119");
  33. static Color Lime = Color.fromHex("bfef45");
  34. static Color Cyan = Color.fromHex("42d4f4");
  35. static Color Purple = Color.fromHex("911eb4");
  36. static Color Magenta = Color.fromHex("f032e6");
  37. static Color Gray = Color.fromHex("a9a9a9");
  38. static Color Pink = Color.fromHex("fabed4");
  39. static Color Apricot = Color.fromHex("ffd8b1");
  40. static Color Biege = Color.fromHex("fffac8");
  41. static Color Mint = Color.fromHex("aaffc3");
  42. static Color Lavender = Color.fromHex("dcbeff");
  43. static Color White = Color.fromHex("ffffff");
  44. static Color terminal() {
  45. return Color(true);
  46. }
  47. private static int hexToDouble(string hex) {
  48. int num;
  49. foreach (indx, hexChar; hex) {
  50. int b10;
  51. if (isDigit(hexChar)) {
  52. b10 = cast(int)(hexChar - '0');
  53. } else {
  54. b10 = cast(int)((toUpper(hexChar) - 0x40) + 9);
  55. }
  56. num += (b10 * pow(16, ((hex.length - indx) - 1)));
  57. }
  58. return num;
  59. }
  60. private static string doubleToHex(int num) {
  61. char[] hex;
  62. for (int i = num; num != 0; num /= 16) {
  63. int remainder = num % 16;
  64. if (remainder > 9) {
  65. hex ~= cast(char)(('A') + (remainder - 10));
  66. } else {
  67. hex ~= cast(int)(remainder + '0');
  68. }
  69. }
  70. return hex.reverse().idup;
  71. }
  72. private static bool validateHexColorFormat(ref string hex) {
  73. if (hex.length == 7 && hex[0] == '#') {
  74. hex = hex[1..7];
  75. }
  76. foreach (hexChar; hex) {
  77. if (!isDigit(hexChar) && (toUpper(hexChar) != 'A' && toUpper(hexChar) != 'B' && toUpper(hexChar) != 'C' && toUpper(hexChar) != 'D' && toUpper(hexChar) != 'E' && toUpper(hexChar) != 'F')) {
  78. return false;
  79. }
  80. }
  81. return true;
  82. }
  83. static Color fromHex(string hex) {
  84. if (!validateHexColorFormat(hex)) {
  85. throw new Exception(format("Invalid format for hex string %s", hex));
  86. }
  87. ubyte r = cast(ubyte)Color.hexToDouble(hex[0..2]);
  88. ubyte g = cast(ubyte)Color.hexToDouble(hex[2..4]);
  89. ubyte b = cast(ubyte)Color.hexToDouble(hex[4..6]);
  90. return Color(r, g, b);
  91. }
  92. string toHex(bool printHashtag = true) {
  93. string r0 = "";
  94. if (doubleToHex(r).length < 2) {
  95. r0 = "0";
  96. }
  97. string g0 = "";
  98. if (doubleToHex(g).length < 2) {
  99. g0 = "0";
  100. }
  101. string b0 = "";
  102. if (doubleToHex(b).length < 2) {
  103. b0 = "0";
  104. }
  105. string hex = r0 ~ doubleToHex(r) ~ g0 ~ doubleToHex(g) ~ b0 ~ doubleToHex(b);
  106. if (printHashtag) {
  107. return '#' ~ hex;
  108. }
  109. return hex;
  110. }
  111. }