From 009ba1418486dde09a1786dc48f2440802b0d3b4 Mon Sep 17 00:00:00 2001 From: Saturneic Date: Wed, 26 Dec 2018 14:31:21 +0800 Subject: [PATCH] Fixed. --- include/hsv.hpp | 19 ++++++++ include/wav.hpp | 16 ++++++- src/fields.cpp | 8 ---- src/hsv.cpp | 113 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 147 insertions(+), 9 deletions(-) create mode 100644 include/hsv.hpp create mode 100644 src/hsv.cpp diff --git a/include/hsv.hpp b/include/hsv.hpp new file mode 100644 index 0000000..c7c2d7e --- /dev/null +++ b/include/hsv.hpp @@ -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 */ diff --git a/include/wav.hpp b/include/wav.hpp index 3cb8730..ca2f14d 100644 --- a/include/wav.hpp +++ b/include/wav.hpp @@ -3,6 +3,7 @@ #include #include +#include using std::string; using std::vector; @@ -153,14 +154,27 @@ public: if(i != bit32.begin()){ 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; + 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; cg = cg>255?255:cg; cb = cb>255?255:cb; sumr += cr; sumb += cb; sumg += cg; + m_color.setColor({cr,cb,cg}); color_data.push_back(m_color); } diff --git a/src/fields.cpp b/src/fields.cpp index 4d06c1c..7c544fb 100644 --- a/src/fields.cpp +++ b/src/fields.cpp @@ -1,9 +1 @@ -// -// fields.cpp -// Fields -// -// Created by 胡一兵 on 2018/12/13. -// Copyright © 2018年 Bakantu. All rights reserved. -// - #include diff --git a/src/hsv.cpp b/src/hsv.cpp new file mode 100644 index 0000000..bd16bb5 --- /dev/null +++ b/src/hsv.cpp @@ -0,0 +1,113 @@ +#include +#include +#include + +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; +}*/