PageRenderTime 44ms CodeModel.GetById 13ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 1ms

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