This commit is contained in:
Saturneic 2018-12-26 14:31:21 +08:00
parent 7d273b1afd
commit 009ba14184
4 changed files with 147 additions and 9 deletions

19
include/hsv.hpp Normal file
View File

@ -0,0 +1,19 @@
#ifndef hsv_h
#define hsv_h
typedef struct {
double r; // a fraction between 0 and 1
double g; // a fraction between 0 and 1
double b; // a fraction between 0 and 1
} rgb;
typedef struct {
double h; // angle in degrees
double s; // a fraction between 0 and 1
double v; // a fraction between 0 and 1
} hsv;
hsv rgb2hsv(rgb in);
rgb hsv2rgb(hsv in);
#endif /* hsv_h */

View File

@ -3,6 +3,7 @@
#include <type.hpp> #include <type.hpp>
#include <graphs.hpp> #include <graphs.hpp>
#include <hsv.hpp>
using std::string; using std::string;
using std::vector; using std::vector;
@ -153,14 +154,27 @@ public:
if(i != bit32.begin()){ if(i != bit32.begin()){
Color m_color; Color m_color;
//printf("%d %d\n",bit.da,bit.db);
double cb = log2(sqrt(bit.da + bit.db)+1)*18.5, cg = log2(sqrt(abs(bit.db - (*(i-1)).db))+1)*42, cr = log2(sqrt(abs(bit.da - (*(i-1)).da))+1)*42; double cb = log2(sqrt(bit.da + bit.db)+1)*18.5, cg = log2(sqrt(abs(bit.db - (*(i-1)).db))+1)*42, cr = log2(sqrt(abs(bit.da - (*(i-1)).da))+1)*42;
rgb crgb;
hsv chsv;
crgb.r = 1.0*cr/255;
crgb.g = 1.0*cg/255;
crgb.b = 1.0*cb/255;
chsv = rgb2hsv(crgb);
//addsaturation(chsv);
chsv.s += 0.5;
chsv.s > 1 ? 1 : 1;
crgb = hsv2rgb(chsv);
cr = 255 * crgb.r;
cg = 255 * crgb.g;
cb = 255 * crgb.b;
cr = cr>255?255:cr; cr = cr>255?255:cr;
cg = cg>255?255:cg; cg = cg>255?255:cg;
cb = cb>255?255:cb; cb = cb>255?255:cb;
sumr += cr; sumr += cr;
sumb += cb; sumb += cb;
sumg += cg; sumg += cg;
m_color.setColor({cr,cb,cg}); m_color.setColor({cr,cb,cg});
color_data.push_back(m_color); color_data.push_back(m_color);
} }

View File

@ -1,9 +1 @@
//
// fields.cpp
// Fields
//
// Created by 胡一兵 on 2018/12/13.
// Copyright © 2018年 Bakantu. All rights reserved.
//
#include <fields.hpp> #include <fields.hpp>

113
src/hsv.cpp Normal file
View File

@ -0,0 +1,113 @@
#include <type.hpp>
#include <graphs.hpp>
#include <hsv.hpp>
hsv rgb2hsv(rgb in)
{
hsv out;
double min, max, delta;
min = in.r < in.g ? in.r : in.g;
min = min < in.b ? min : in.b;
max = in.r > in.g ? in.r : in.g;
max = max > in.b ? max : in.b;
out.v = max; // v
delta = max - min;
if (delta < 0.00001)
{
out.s = 0;
out.h = 0; // undefined, maybe nan?
return out;
}
if (max > 0.0) { // NOTE: if Max is == 0, this divide would cause a crash
out.s = (delta / max); // s
}
else {
// if max is 0, then r = g = b = 0
// s = 0, h is undefined
out.s = 0.0;
out.h = NAN; // its now undefined
return out;
}
if (in.r >= max) // > is bogus, just keeps compilor happy
out.h = (in.g - in.b) / delta; // between yellow & magenta
else
if (in.g >= max)
out.h = 2.0 + (in.b - in.r) / delta; // between cyan & yellow
else
out.h = 4.0 + (in.r - in.g) / delta; // between magenta & cyan
out.h *= 60.0; // degrees
if (out.h < 0.0)
out.h += 360.0;
return out;
}
rgb hsv2rgb(hsv in)
{
double hh, p, q, t, ff;
long i;
rgb out;
if (in.s <= 0.0) { // < is bogus, just shuts up warnings
out.r = in.v;
out.g = in.v;
out.b = in.v;
return out;
}
hh = in.h;
if (hh >= 360.0) hh = 0.0;
hh /= 60.0;
i = (long)hh;
ff = hh - i;
p = in.v * (1.0 - in.s);
q = in.v * (1.0 - (in.s * ff));
t = in.v * (1.0 - (in.s * (1.0 - ff)));
switch (i) {
case 0:
out.r = in.v;
out.g = t;
out.b = p;
break;
case 1:
out.r = q;
out.g = in.v;
out.b = p;
break;
case 2:
out.r = p;
out.g = in.v;
out.b = t;
break;
case 3:
out.r = p;
out.g = q;
out.b = in.v;
break;
case 4:
out.r = t;
out.g = p;
out.b = in.v;
break;
case 5:
default:
out.r = in.v;
out.g = p;
out.b = q;
break;
}
return out;
}
/*
void addsaturation(hsv &in) {
in.s += 0.1;
in.s > 1 ? 1 : 1;
return;
}*/