From e2a2ffd2f557320eb6dcaf4904604d07c426b067 Mon Sep 17 00:00:00 2001 From: xianjimli Date: Fri, 7 Dec 2018 18:00:04 +0800 Subject: [PATCH] improve agge linear_gradient --- 3rd/agge/agge/blender_linear_gradient.h | 26 ++++++++++++++++++------- docs/changes.md | 2 +- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/3rd/agge/agge/blender_linear_gradient.h b/3rd/agge/agge/blender_linear_gradient.h index a306329dc..6af9b010d 100644 --- a/3rd/agge/agge/blender_linear_gradient.h +++ b/3rd/agge/agge/blender_linear_gradient.h @@ -22,7 +22,7 @@ class blender_linear_gradient { float _ey; float _dx; float _dy; - float _d; + float _dot_product_1; float _xw; float _yw; pixel32_rgba _sc; @@ -32,11 +32,15 @@ class blender_linear_gradient { template inline blender_linear_gradient::blender_linear_gradient(float sx, float sy, float ex, float ey, pixel32_rgba sc, pixel32_rgba ec) : _sx(sx), _sy(sy), _ex(ex), _ey(ey), _sc(sc), _ec(ec) { + if(sx == ex && sy == ey) { + assert(!"invalid params"); + _ex = _sx + 1; + _ey = _sy + 1; + } + _dx = ex - sx; _dy = ey - sy; - _xw = _dx / (_dx + _dy); - _yw = _dy / (_dx + _dy); - _d = sqrt(_dx * _dx + _dy * _dy); + _dot_product_1 = 1/(_dx * _dx + _dy * _dy); } template @@ -65,9 +69,17 @@ inline bool blender_linear_gradient::get_color(int x, int y, pixel32_rgb } else if(_sy == _ey) { return this->gradient((x - _sx)/_dx, c); } else { - /*TODO*/ - c = _ec; - return true; + //https://github.com/SFML/SFML/wiki/Source:-Color-Gradient + if(x < _sx || y < _sy) { + c = _sc; + } else if(x > _ex || y > _ey) { + c = _ec; + } else { + float dot_product = (x - _sx) * _dx + (y - _sy) * _dy; + float factor = dot_product * _dot_product_1; + + return this->gradient(factor, c); + } } return true; diff --git a/docs/changes.md b/docs/changes.md index b6d6c6c31..8b7d9eb14 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -1,6 +1,6 @@ # 最新动态 * 2018/12/07 - * VGCanvas支持渐变色(GL版本OK,AGGE版本还需要完善,AGG版本暂时不支持)。 + * VGCanvas支持渐变色(GL版本OK,AGGE版本OK,AGG版本暂时不支持)。 * demoui增加vgcanvas的demo。 * draw\_vline高度允许为负。 * draw\_hline宽度允许为负。