Subversion Repositories public iLand

Rev

Rev 1221 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1211 werner 1
/********************************************************************************************
2
**    iLand - an individual based forest landscape and disturbance model
3
**    http://iland.boku.ac.at
4
**    Copyright (C) 2009-  Werner Rammer, Rupert Seidl
5
**
6
**    This program is free software: you can redistribute it and/or modify
7
**    it under the terms of the GNU General Public License as published by
8
**    the Free Software Foundation, either version 3 of the License, or
9
**    (at your option) any later version.
10
**
11
**    This program is distributed in the hope that it will be useful,
12
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
**    GNU General Public License for more details.
15
**
16
**    You should have received a copy of the GNU General Public License
17
**    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
********************************************************************************************/
877 werner 19
#include "colors.h"
1009 werner 20
#include "dem.h"
877 werner 21
 
22
QVector<QColor> Colors::mBrewerDiv = QVector<QColor>() << QColor("#543005") << QColor("#8c510a") << QColor("#bf812d") << QColor("#dfc27d")
23
                                                       << QColor("#f6e8c3") << QColor("#f5f5f5") << QColor("#fdbf6f") << QColor("##c7eae5")
24
                                                       << QColor("#80cdc1") << QColor("#35978f") << QColor("#01665e") <<  QColor("#003c30");
25
 
26
QVector<QColor> Colors::mBrewerQual = QVector<QColor>() << QColor("#a6cee3") << QColor("#1f78b4") << QColor("#b2df8a") << QColor("#33a02c")
27
                                                       << QColor("#fb9a99") << QColor("#e31a1c") << QColor("#fdbf6f") << QColor("#ff7f00")
28
                                                       << QColor("#cab2d6") << QColor("#6a3d9a") << QColor("#ffff99") <<  QColor("#b15928");
29
 
30
 
31
QVector<QColor> Colors::mTerrainCol = QVector<QColor>() << QColor("#00A600") << QColor("#24B300") << QColor("#4CBF00") << QColor("#7ACC00")
32
                                                       << QColor("#ADD900") << QColor("#E6E600") << QColor("#E8C727") << QColor("#EAB64E")
33
                                                       << QColor("#ECB176") << QColor("#EEB99F") << QColor("#F0CFC8") <<  QColor("#F2F2F2");
34
 
879 werner 35
void Colors::setPalette(const GridViewType type, const float min_val, const float max_val)
877 werner 36
{
1041 werner 37
    if (mNeedsPaletteUpdate==false && type==mCurrentType &&
38
            (mAutoScale==false || (minValue()==min_val && maxValue()==max_val))  )
880 werner 39
        return;
40
 
41
    mHasFactors = false;
879 werner 42
    int n = 50;
880 werner 43
    if (type >= GridViewBrewerDiv) {
44
        // categorical values...
45
        mHasFactors = true;
46
        n=mFactorLabels.size();
47
        if (mFactorLabels.isEmpty()) {
48
            n=max_val;
49
            mFactorLabels.clear();
50
            for (int i=0;i<n;++i)
51
                mFactorLabels.append(QString("Label %1").arg(i));
52
        }
53
    }
54
    if (type != GridViewCustom) {
55
        mColors.clear();
56
        for (int i=0;i<n;++i)
57
            if (mHasFactors)
58
                mColors.append(colorFromValue(i, type, 0., 1.).name());
59
            else
60
                mColors.append(colorFromValue(1. - i/double(n), type, 0., 1.).name());
879 werner 61
 
880 werner 62
    }
879 werner 63
    mLabels = QStringList() << QString::number(min_val)
890 werner 64
                            << QString::number((3.*min_val + max_val)/4.)
65
                            << QString::number((min_val+max_val)/2.)
66
                            << QString::number((min_val + 3.*max_val)/4.)
880 werner 67
                            << QString::number(max_val);
68
 
69
    if (mAutoScale) {
70
        mMinValue = min_val;
71
        mMaxValue = max_val;
72
    }
73
    mCurrentType = type;
74
    mNeedsPaletteUpdate = false;
879 werner 75
    emit colorsChanged();
877 werner 76
}
77
 
880 werner 78
void Colors::setFactorLabels(QStringList labels)
79
{
80
    mFactorLabels = labels;
81
    mNeedsPaletteUpdate = true;
82
}
83
 
879 werner 84
Colors::Colors(QWidget *parent): QObject(parent)
85
{
880 werner 86
    mNeedsPaletteUpdate =true;
87
    mAutoScale = true;
88
    mHasFactors = false;
89
    mMeterPerPixel = 1.;
90
    //default start palette
91
    //setPalette(GridViewRainbow, 0, 1);
92
    // factors test
1054 werner 93
    setCaption("");
880 werner 94
    setPalette(GridViewTerrain, 0, 4);
879 werner 95
}
96
 
877 werner 97
QColor Colors::colorFromPalette(const int value, const GridViewType view_type)
98
{
882 werner 99
    if (value<0)
100
        return Qt::white;
877 werner 101
    int n = qMax(value,0) % 12;
102
    QColor col;
103
    switch(view_type) {
104
    case GridViewBrewerDiv: col = mBrewerDiv[n]; break;
105
    case GridViewBrewerQual: col = mBrewerQual[n]; break;
106
    case GridViewTerrain: col = mTerrainCol[n]; break;
107
    default: return QColor();
108
    }
109
    if (value < 12)
110
        return col;
111
    n = qMax(value,0) % 60;
112
    if (n<12) return col;
113
    if (n<24) return col.darker(200);
114
    if (n<36) return col.lighter(150);
115
    if (n<48) return col.darker(300);
116
    return col.lighter(200);
117
 
118
}
119
 
1009 werner 120
QColor Colors::shadeColor(const QColor col, const QPointF &coordinates, const DEM *dem)
121
{
122
    if (dem) {
123
        float val = dem->viewGrid()->constValueAt(coordinates); // scales from 0..1
124
        double h, s, v;
125
        col.getHsvF(&h, &s, &v);
126
        // we adjust the 'v', the lightness: if val=0.5 -> nothing changes
127
        v=limit( v - (1.-val)*0.4, 0.1, 1.);
128
        QColor c;
129
        c.setHsvF(h,s,v);
130
        return c;
131
 
132
    } else
133
        return col;
134
}
135
 
877 werner 136
// colors
137
QColor Colors::colorFromValue(const float value,
138
                              const float min_value, const float max_value,
139
                              const bool reverse, const bool black_white)
140
{
141
    float rval = value;
142
    rval = std::max(min_value, rval);
143
    rval = std::min(max_value, rval);
144
    if (reverse)
145
        rval = max_value - rval;
146
    float rel_value;
147
    QColor col;
148
    if (min_value < max_value) {
149
        // default: high values -> red (h=0), low values to blue (h=high)
150
        rel_value = 1 - (rval - min_value) / (max_value - min_value);
151
        if (black_white) {
152
            int c = (1.-rel_value)*255;
153
            col = QColor(c,c,c);
154
        } else
155
            col=  QColor::fromHsvF(0.66666666666*rel_value, 0.95, 0.95).rgb();
156
    } else {
157
        col = Qt::white;
158
    }
159
    return col;
160
}
161
 
162
QColor Colors::colorFromValue(const float value, const GridViewType view_type, const float min_value, const float max_value)
163
{
164
    if (view_type==GridViewGray || view_type==GridViewGrayReverse)
165
        return colorFromValue(value, min_value, max_value, view_type==GridViewGrayReverse, true);
166
 
167
    if (view_type==GridViewRainbow || view_type==GridViewRainbowReverse)
168
        return colorFromValue(value, min_value, max_value, view_type==GridViewRainbowReverse, false);
169
 
1040 werner 170
    if (view_type == GridViewGreens || view_type==GridViewBlues || view_type==GridViewReds) {
171
        float rval = value;
172
        rval = std::max(min_value, rval);
173
        rval = std::min(max_value, rval);
174
        float rel_value = (max_value!=min_value)?(rval - min_value) / (max_value - min_value): 0;
175
        int r,g,b;
176
        switch (view_type) {
1157 werner 177
        case GridViewGreens:  // 11,111,19
178
            r=220 - rel_value*(220-11); g=220-rel_value*(220-111); b=220-rel_value*(220-19); break;
179
        case GridViewBlues: //15,67,138
180
            r=220 - rel_value*(220-15); g=220-rel_value*(220-67); b=220-rel_value*(220-138); break;
181
        case GridViewReds: //219,31,72
182
            r=240 - rel_value*(220-219); g=240-rel_value*(220-31); b=240-rel_value*(220-72); break;
1040 werner 183
        default: r=g=b=0;
184
        }
185
        return QColor(r,g,b);
186
 
187
    }
877 werner 188
    if (view_type == GridViewHeat) {
189
        float rval = value;
190
        rval = std::max(min_value, rval);
191
        rval = std::min(max_value, rval);
192
        float rel_value = 1 - (rval - min_value) / (max_value - min_value);
193
        int g=255, b=0;
194
        if (rel_value < 0.5)
195
            g = rel_value*2.f * 255;
196
        if (rel_value>0.5)
197
            b = (rel_value-0.5)*2.f * 255;
198
        return QColor(255,g,b);
199
 
200
    }
201
    return colorFromPalette(value, view_type);
202
 
203
 
204
}