/inc/utils.h

https://code.google.com/p/dwarftherapist/ · C Header · 160 lines · 118 code · 19 blank · 23 comment · 9 complexity · f0eae67003b7210db4aebf2af6935b03 MD5 · raw file

  1. /*
  2. Dwarf Therapist
  3. Copyright (c) 2009 Trey Stout (chmod)
  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. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #ifndef UTILS_H
  21. #define UTILS_H
  22. #include <QByteArray>
  23. #include <QColor>
  24. #include <QtGlobal>
  25. // valid for as long as DF stays 32bit
  26. typedef quint32 VIRTADDR;
  27. typedef quint8 BYTE;
  28. typedef quint16 WORD;
  29. static inline QByteArray encode_short(short num) {
  30. char *bytes;
  31. bytes = (char*)&num;
  32. QByteArray arr(bytes, sizeof(short));
  33. return arr;
  34. }
  35. static inline QByteArray encode(int num) {
  36. char *bytes;
  37. bytes = (char*)&num;
  38. QByteArray arr(bytes, sizeof(int));
  39. return arr;
  40. }
  41. static inline QByteArray encode(VIRTADDR num) {
  42. char *bytes;
  43. bytes = (char*)&num;
  44. QByteArray arr(bytes, sizeof(uint));
  45. return arr;
  46. }
  47. static inline QByteArray encode(const ushort &num) {
  48. char *bytes;
  49. bytes = (char*)&num;
  50. QByteArray arr(bytes, sizeof(ushort));
  51. return arr;
  52. }
  53. static inline BYTE decode_byte(const QByteArray &arr) {
  54. BYTE *out_ptr = (BYTE*)arr.constData();
  55. return *out_ptr;
  56. }
  57. static inline WORD decode_word(const QByteArray &arr) {
  58. WORD *out_ptr = (WORD*)arr.constData();
  59. return *out_ptr;
  60. }
  61. static inline quint32 decode_dword(const QByteArray &arr) {
  62. quint32 *out_ptr = (quint32*)arr.constData();
  63. return *out_ptr;
  64. }
  65. static inline int decode_int(const QByteArray &arr) {
  66. const char* data = arr.constData();
  67. int *out_ptr = (int*)data;
  68. return *out_ptr;
  69. }
  70. static inline short decode_short(const QByteArray &arr) {
  71. short *out_ptr = (short*)arr.constData();
  72. return *out_ptr;
  73. }
  74. static inline QByteArray encode_skillpattern(short skill, short exp, short rating) {
  75. QByteArray bytes;
  76. bytes.reserve(6);
  77. bytes[0] = (uchar)skill;
  78. bytes[1] = (uchar)(skill >> 8);
  79. bytes[2] = (uchar)exp;
  80. bytes[3] = (uchar)(exp >> 8);
  81. bytes[4] = (uchar)rating;
  82. bytes[5] = (uchar)(rating >> 8);
  83. return bytes;
  84. }
  85. static inline QString by_char(QByteArray arr) {
  86. QString out;
  87. for(int i=0; i < arr.size(); i++) {
  88. out += QString::number((uchar)arr.at(i), 16);
  89. out += " ";
  90. }
  91. return out;
  92. }
  93. static inline QColor compliment(const QColor &in_color) {
  94. qreal brightness = (in_color.red() * 299 + in_color.green() * 587 + in_color.blue() * 114) / 255000.0;
  95. QColor tmp = in_color.toHsv();
  96. int h = tmp.hue();
  97. int s = 25;
  98. int v;
  99. if (brightness >= 0.5) {
  100. v = 0;
  101. } else {
  102. v = 255;
  103. }
  104. return QColor::fromHsv(h, s, v);
  105. }
  106. static inline QColor from_hex(const QString &h) {
  107. bool ok;
  108. QColor retval = Qt::gray;
  109. if (h.length() == 8) { // "0x99AABB" (no alpha)
  110. retval = QColor(h.toInt(&ok, 16));
  111. } else if (h.length() == 10) { // "0x99AABBFF" (last two for alpha channel)
  112. int r = h.mid(2, 2).toInt(&ok, 16);
  113. int g = h.mid(4, 2).toInt(&ok, 16);
  114. int b = h.mid(6, 2).toInt(&ok, 16);
  115. int a = h.mid(8, 2).toInt(&ok, 16);
  116. retval = QColor(r, g, b, a);
  117. }
  118. return retval;
  119. }
  120. static inline QString to_hex(const QColor &c) {
  121. return QString("0x%1%2%3%4")
  122. .arg(c.red(), 2, 16, QChar('0'))
  123. .arg(c.green(), 2, 16, QChar('0'))
  124. .arg(c.blue(), 2, 16, QChar('0'))
  125. .arg(c.alpha(), 2, 16, QChar('0'));
  126. }
  127. static inline QString hexify(const uint &num) {
  128. return QString("0x%1").arg(num, 8, 16, QChar('0'));
  129. }
  130. static inline QString capitalize(const QString & word) {
  131. QString result = word;
  132. if(!result.isEmpty()) {
  133. result = result.toLower();
  134. result[0] = result[0].toUpper();
  135. }
  136. return result;
  137. }
  138. #endif // UTILS_H