/CS/migrated/branches/NEW_SHADERSYS/libs/csgfx/gradient.cpp
# · C++ · 129 lines · 87 code · 20 blank · 22 comment · 9 complexity · 7752f60964b92f122cd5e984500f6d84 MD5 · raw file
- /*
- Copyright (C) 2003 by Jorrit Tyberghein
- (C) 2003 by Frank Richter
- This library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Library General Public
- License as published by the Free Software Foundation; either
- version 2 of the License, or (at your option) any later version.
- This library 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
- Library General Public License for more details.
- You should have received a copy of the GNU Library General Public
- License along with this library; if not, write to the Free
- Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
- #include "cssysdef.h"
- #include "qint.h"
- #include "csgfx/gradient.h"
- csGradientShade::csGradientShade() :
- left (0, 0, 0), right (0, 0, 0), position (0)
- {
- }
- csGradientShade::csGradientShade (csColor left_color, csColor right_color,
- float pos) :
- left (left_color), right (right_color), position (pos)
- {
- }
- csGradientShade::csGradientShade (csColor color, float pos) :
- left (color), right (color), position (pos)
- {
- }
- csGradient::csGradient()
- {
- }
- csGradient::csGradient (csColor first, csColor last)
- {
- AddShade (csGradientShade (first, 0.0f));
- AddShade (csGradientShade (last, 1.0f));
- }
- void csGradient::AddShade (csGradientShade shade)
- {
- int lo = 0, hi = shades.Length() - 1;
- int mid = 0;
- while (lo <= hi)
- {
- mid = (lo + hi) / 2;
- if (shades[mid].position < shade.position)
- {
- lo = mid + 1;
- }
- else
- {
- hi = mid - 1;
- }
- }
- shades.Insert (lo, shade);
- }
- void csGradient::Clear ()
- {
- shades.DeleteAll ();
- }
- #define CLAMP(x) ((x<EPSILON)?0.0f:(((x-1.0f)>EPSILON)?1.0f:x))
- bool csGradient::Render (csRGBcolor* pal, int count,
- float begin, float end) const
- {
- if (shades.Length() == 0) return false;
- // current color
- csColor color = shades[0].left;
- // delta per palette item
- csColor delta (0, 0, 0);
- // step in the gradient per pal item
- float step = (end - begin) / (float)count;
- float gradpos = begin;
- // current shade index
- int csi = 0;
- const csGradientShade* currshade = 0;
- const csGradientShade* nextshade = &shades[0];
- for (int i = 0; i < count; i++)
- {
- while (csi < shades.Length() &&
- (gradpos >= nextshade->position))
- {
- currshade = nextshade;
- csi++;
- if (csi < shades.Length())
- {
- nextshade = &shades[csi];
- }
- color = (step > EPSILON)? currshade->right : currshade->left;
- delta = (((step > EPSILON)? nextshade->left : nextshade->right) - color);
- float diff = (nextshade->position - currshade->position);
- if (ABS (diff) > EPSILON)
- {
- color +=
- (delta * ((gradpos - currshade->position) / diff));
- delta *= (step / diff);
- }
- }
- pal[i].red = QInt (CLAMP (color.red) * 255.99f);
- pal[i].green = QInt (CLAMP (color.green) * 255.99f);
- pal[i].blue = QInt (CLAMP (color.blue) * 255.99f);
- color += delta;
- gradpos += step;
- }
- return true;
- }