From d893f081439e56b2a8570d8600940c8210c63df4 Mon Sep 17 00:00:00 2001 From: "M. Liebmann" Date: Sat, 20 Sep 2014 21:34:01 +0200 Subject: [PATCH] Add src/driver/colorgradient.cpp --- src/driver/Makefile.am | 1 + src/driver/colorgradient.cpp | 181 +++++++++++++++++++++++++++++++++++ src/driver/colorgradient.h | 58 +++++++++++ 3 files changed, 240 insertions(+) create mode 100644 src/driver/colorgradient.cpp create mode 100644 src/driver/colorgradient.h diff --git a/src/driver/Makefile.am b/src/driver/Makefile.am index 7bbc69b96..30a86f5a1 100644 --- a/src/driver/Makefile.am +++ b/src/driver/Makefile.am @@ -21,6 +21,7 @@ libneutrino_driver_a_SOURCES = \ audiofile.cpp \ audiometadata.cpp \ audioplay.cpp \ + colorgradient.cpp \ fade.cpp \ fb_window.cpp \ file.cpp \ diff --git a/src/driver/colorgradient.cpp b/src/driver/colorgradient.cpp new file mode 100644 index 000000000..3fc8c41b7 --- /dev/null +++ b/src/driver/colorgradient.cpp @@ -0,0 +1,181 @@ +/* + color gradient - Neutrino-GUI + Copyright (C) 2014 M. Liebmann (micha-bbg) + + License: GPL + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public + License along with this program; if not, write to the + Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifdef HAVE_CONFIG_H +#include +#endif +#include +#include +#include +#include +#include + +#pragma GCC diagnostic error "-Wconversion" + +CColorGradient::CColorGradient() +{ + frameBuffer = CFrameBuffer::getInstance(); +} + +CColorGradient::~CColorGradient() +{ +} + +#if 0 +CColorGradient* CColorGradient::getInstance() +{ + static CColorGradient* GradientInstance = NULL; + if (!GradientInstance) + GradientInstance = new CColorGradient(); + return GradientInstance; +} +#endif + + +uint8_t CColorGradient::limitChar(int c) +{ + uint8_t ret; + if (c < 0) ret = 0; + else if (c > 0xFF) ret = 0xFF; + else ret = (uint8_t)c; + return ret; +} + +fb_pixel_t* CColorGradient::gradientColorToTransparent(fb_pixel_t col, fb_pixel_t *gradientBuf, int bSize, int /*mode*/, int /*intensity*/) +{ + if (gradientBuf == NULL) { + gradientBuf = (fb_pixel_t*) malloc(bSize * sizeof(fb_pixel_t)); + if (gradientBuf == NULL) { + dprintf(DEBUG_NORMAL, "[%s:%d] malloc error\n", __func__, __LINE__); + return NULL; + } + } + memset((void*)gradientBuf, '\0', bSize * sizeof(fb_pixel_t)); + + int start_box = 0; + int end_box = bSize; + uint8_t tr_min = 0xFF; + uint8_t tr_max = 0x20; + float factor = (float)(tr_min - tr_max) / (float)(end_box - start_box); + + for (int i = start_box; i < end_box; i++) { + + uint8_t tr = limitChar((int)(factor * (float)i + tr_max) + 1); + uint8_t r = (uint8_t)((col & 0x00FF0000) >> 16); + uint8_t g = (uint8_t)((col & 0x0000FF00) >> 8); + uint8_t b = (uint8_t) (col & 0x000000FF); + + gradientBuf[i] = ((tr << 24) & 0xFF000000) | + ((r << 16) & 0x00FF0000) | + ((g << 8) & 0x0000FF00) | + ( b & 0x000000FF); + } + return gradientBuf; +} + +fb_pixel_t* CColorGradient::gradientOneColor(fb_pixel_t col, fb_pixel_t *gradientBuf, int bSize, int mode, int intensity) +{ + + if (gradientBuf == NULL) { + gradientBuf = (fb_pixel_t*) malloc(bSize * sizeof(fb_pixel_t)); + if (gradientBuf == NULL) { + dprintf(DEBUG_NORMAL, "[%s:%d] malloc error\n", __func__, __LINE__); + return NULL; + } + } + memset((void*)gradientBuf, '\0', bSize * sizeof(fb_pixel_t)); + + HsvColor hsv; + uint8_t min_v=0, max_v=0, min_s=0, max_s=0; + uint8_t start_v=0 , end_v=0, start_s=0, end_s=0; + + uint8_t tr = SysColor2Hsv(col, &hsv); + bool noSaturation = (hsv.s <= (float)0.05); + + switch (intensity) { + case light: + min_v = limitChar((int)(hsv.v * 255 - hsv.v * 255/30)); + max_v = 0xE0; + min_s = limitChar((int)((hsv.s * 255) / 1.5)); + max_s = 0xE0; + break; + case normal: + min_v = limitChar((int)(hsv.v * 255 - hsv.v * 255/3)); + max_v = 0xFF; + min_s = limitChar((int)((hsv.s * 255) / 2.5)); + max_s = 0xFF; + break; + } + + switch (mode) { + case gradientDark2Light: + case gradientDark2Light2Dark: + start_v = min_v; + end_v = max_v; + start_s = max_s; + end_s = min_s; + break; + case gradientLight2Dark: + case gradientLight2Dark2Light: + start_v = max_v; + end_v = min_v; + start_s = min_s; + end_s = max_s; + break; + default: + return 0; + } + + int bSize1 = ((mode == gradientDark2Light2Dark) || (mode == gradientLight2Dark2Light)) ? bSize/2 : bSize; + + int v = start_v; int v_ = v; + float factor_v = ((float)end_v - (float)v) / (float)bSize1; + int s = start_s; int s_ = s; + float factor_s = ((float)end_s - (float)s) / (float)bSize1; + + for (int i = 0; i < bSize1; i++) { + v = v_ + (int)(factor_v * (float)i); + hsv.v = (float)limitChar(v) / (float)255; + if (!noSaturation) { + s = s_ + (int)(factor_s * (float)i); + hsv.s = (float)limitChar(s) / (float)255; + } + gradientBuf[i] = Hsv2SysColor(&hsv, tr); + } + + if ((mode == gradientDark2Light2Dark) || (mode == gradientLight2Dark2Light)) { + bSize1 = bSize - bSize1; + for (int i = 0; i < bSize1; i++) { + v = v_ + (int)(factor_v * (float)i); + hsv.v = (float)limitChar(v) / (float)255; + if (!noSaturation) { + s = s_ + (int)(factor_s * (float)i); + hsv.s = (float)limitChar(s) / (float)255; + } + gradientBuf[bSize-i-1] = Hsv2SysColor(&hsv, tr); + } + } + + return gradientBuf; +} + +//printf("[%s #%d] factor_v: %f, factor_s: %f, v: 0x%02X, s: 0x%02X\n", __func__, __LINE__, factor_v, factor_s, v, s); diff --git a/src/driver/colorgradient.h b/src/driver/colorgradient.h new file mode 100644 index 000000000..5f6473776 --- /dev/null +++ b/src/driver/colorgradient.h @@ -0,0 +1,58 @@ +/* + color gradient - Neutrino-GUI + Copyright (C) 2014 M. Liebmann (micha-bbg) + + License: GPL + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public + License along with this program; if not, write to the + Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef __CCOLORGRADIENT__ +#define __CCOLORGRADIENT__ + +#include + +class CColorGradient +{ + private: + CFrameBuffer * frameBuffer; + + inline uint8_t limitChar(int c); + + public: + + enum { + gradientDark2Light, + gradientLight2Dark, + gradientDark2Light2Dark, + gradientLight2Dark2Light + }; + + enum { + light, + normal + }; + + CColorGradient(); + ~CColorGradient(); +// static CColorGradient* getInstance(); + + fb_pixel_t* gradientOneColor(fb_pixel_t col, fb_pixel_t *gradientBuf, int bSize, int mode, int intensity=normal); + fb_pixel_t* gradientColorToTransparent(fb_pixel_t col, fb_pixel_t *gradientBuf, int bSize, int mode, int intensity=normal); + +}; + +#endif // __CCOLORGRADIENT__