Skip to content

Commit

Permalink
migrate Block and Biome to generic color names
Browse files Browse the repository at this point in the history
Up to now only the Entities used the flexible CSS standardized color
format. This commit brings this also to Blocks and Bioms.
+ now using QColor
+ replaced complicated color parsing code with QColor::setNamedColor()
method
+ simplified calculation of light spectrum
(Transparent blocks are brighter now. This seems to be a bug in the
current version, as transparent colors are darkened.)
  • Loading branch information
EtlamGit committed Nov 21, 2016
1 parent 438d56b commit 68b9ce5
Show file tree
Hide file tree
Showing 7 changed files with 574 additions and 599 deletions.
64 changes: 25 additions & 39 deletions biomeidentifier.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/** Copyright (c) 2013, Sean Kasun */

#include <assert.h>

#include "./biomeidentifier.h"
#include "./json.h"

Expand Down Expand Up @@ -58,47 +60,31 @@ int BiomeIdentifier::addDefinitions(JSONArray *defs, int pack) {
else
biome->name = "Unknown";

if (b->has("color")) {
QString color = b->at("color")->asString();
quint32 col = 0;
for (int h = 0; h < color.length(); h++) {
ushort c = color.at(h).unicode();
col <<= 4;
if (c >= '0' && c <= '9')
col |= c - '0';
else if (c >= 'A' && c <= 'F')
col |= c - 'A' + 10;
else if (c >= 'a' && c <= 'f')
col |= c - 'a' + 10;
}
int rd = col >> 16;
int gn = (col >> 8) & 0xff;
int bl = col & 0xff;

if (b->has("alpha"))
biome->alpha = b->at("alpha")->asNumber();
else
biome->alpha = 1.0;

// pre multiply alphas
rd *= biome->alpha;
gn *= biome->alpha;
bl *= biome->alpha;
// check for "alpha" (0: transparent / 1: saturated)
// probably never used
if (b->has("alpha"))
biome->alpha = b->at("alpha")->asNumber();
else
biome->alpha = 1.0;

// pre-calculate light spectrum
double y = 0.299 * rd + 0.587 * gn + 0.114 * bl;
double u = (bl - y) * 0.565;
double v = (rd - y) * 0.713;
double delta = y / 15;
for (int i = 0; i < 16; i++) {
y = i * delta;
rd = (unsigned int)clamp(y + 1.403 * v, 0, 255);
gn = (unsigned int)clamp(y - 0.344 * u - 0.714 * v, 0, 255);
bl = (unsigned int)clamp(y + 1.770 * u, 0, 255);
biome->colors[i] = (rd << 16) | (gn << 8) | bl;
}
// get color definition
QColor biomecolor;
if (b->has("color")) {
QString colorname = b->at("color")->asString();
biomecolor.setNamedColor(colorname);
assert(biomecolor.isValid());
} else {
biome->alpha = 0.0;
// use hashed by name instead
quint32 hue = qHash(biome->name);
biomecolor.setHsv(hue % 360, 255, 255);
}

// pre-calculate light spectrum
for (int i = 0; i < 16; i++) {
biome->colors[i].setHsv( biomecolor.hue(),
biomecolor.saturation(),
biomecolor.value()*(i/16.0),
255*biome->alpha );
}

biomes[id].append(biome);
Expand Down
3 changes: 2 additions & 1 deletion biomeidentifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
#include <QHash>
#include <QList>
#include <QString>
#include <QColor>
class JSONArray;

class BiomeInfo {
public:
BiomeInfo() {}
QString name;
bool enabled;
quint32 colors[16];
QColor colors[16];
double alpha;
};

Expand Down
75 changes: 31 additions & 44 deletions blockidentifier.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/** Copyright (c) 2013, Sean Kasun */

#include <QDebug>
#include <assert.h>

#include "./blockidentifier.h"
#include "./json.h"

Expand Down Expand Up @@ -207,53 +209,38 @@ void BlockIdentifier::parseDefinition(JSONObject *b, BlockInfo *parent,
else
block->providepower = false;

if (b->has("alpha"))
block->alpha = b->at("alpha")->asNumber();
else if (parent != NULL)
block->alpha = parent->alpha;
else
block->alpha = 1.0;

QColor blockcolor;
if (b->has("color")) {
QString color = b->at("color")->asString();
quint32 col = 0;
for (int h = 0; h < color.length(); h++) {
ushort c = color.at(h).unicode();
col <<= 4;
if (c >= '0' && c <= '9')
col |= c - '0';
else if (c >= 'A' && c <= 'F')
col |= c - 'A' + 10;
else if (c >= 'a' && c <= 'f')
col |= c - 'a' + 10;
}
int rd = col >> 16;
int gn = (col >> 8) & 0xff;
int bl = col & 0xff;

if (b->has("alpha"))
block->alpha = b->at("alpha")->asNumber();
else if (parent != NULL)
block->alpha = parent->alpha;
else
block->alpha = 1.0;

// pre multiply alphas
rd *= block->alpha;
gn *= block->alpha;
bl *= block->alpha;

// pre-calculate light spectrum
double y = 0.299 * rd + 0.587 * gn + 0.114 * bl;
double u = (bl - y) * 0.565;
double v = (rd - y) * 0.713;
double delta = y / 15;
for (int i = 0; i < 16; i++) {
y = i * delta;
rd = (unsigned int)clamp(y + 1.403 * v, 0, 255);
gn = (unsigned int)clamp(y - 0.344 * u - 0.714 * v, 0, 255);
bl = (unsigned int)clamp(y + 1.770 * u, 0, 255);
block->colors[i] = (rd << 16) | (gn << 8) | bl;
}
QString colorname = b->at("color")->asString();
blockcolor.setNamedColor(colorname);
assert(blockcolor.isValid());
} else if (parent != NULL) {
for (int i = 0; i < 16; i++)
block->colors[i] = parent->colors[i];
block->alpha = parent->alpha;
// copy brightest color from parent
blockcolor = parent->colors[15];
} else {
block->alpha = 0.0;
// use hashed by name instead
quint32 hue = qHash(block->getName());
blockcolor.setHsv(hue % 360, 255, 255);
}

// pre multiply alphas
// rd *= block->alpha;
// gn *= block->alpha;
// bl *= block->alpha;

// pre-calculate light spectrum
for (int i = 0; i < 16; i++) {
block->colors[i].setHsv( blockcolor.hue(),
blockcolor.saturation(),
blockcolor.value()*(i/15.0),
255*block->alpha );
}

if (b->has("mask"))
Expand Down
3 changes: 2 additions & 1 deletion blockidentifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <QMap>
#include <QHash>
#include <QList>
#include <QColor>

class JSONArray;
class JSONObject;
Expand Down Expand Up @@ -47,7 +48,7 @@ class BlockInfo {
bool rendernormal;
bool providepower;
bool spawninside;
quint32 colors[16];
QColor colors[16];

private:
QString name;
Expand Down
Loading

0 comments on commit 68b9ce5

Please sign in to comment.