diff options
author | Graeme Geldenhuys <graemeg@gmail.com> | 2009-09-27 21:48:18 +0200 |
---|---|---|
committer | Graeme Geldenhuys <graemeg@gmail.com> | 2009-09-27 23:09:03 +0200 |
commit | eb41be96437129fda14cd043d8d2fdfb97cc11d4 (patch) | |
tree | e5aec603aa10916ed8f5d100e835aa011374a354 /src/gui/fpg_colormapping.pas | |
parent | 1dfb899821861c36e0c16938f8a15215e8491d19 (diff) | |
download | fpGUI-eb41be96437129fda14cd043d8d2fdfb97cc11d4.tar.xz |
ColorWheel and ValueBar implementation including color mapping routines.
These components will allow us to implement a color selection dialog.
Diffstat (limited to 'src/gui/fpg_colormapping.pas')
-rw-r--r-- | src/gui/fpg_colormapping.pas | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/src/gui/fpg_colormapping.pas b/src/gui/fpg_colormapping.pas new file mode 100644 index 00000000..9f486bbf --- /dev/null +++ b/src/gui/fpg_colormapping.pas @@ -0,0 +1,138 @@ +{ + fpGUI - Free Pascal GUI Library + + Copyright (C) 2006 - 2009 See the file AUTHORS.txt, included in this + distribution, for details of the copyright. + + See the file COPYING.modifiedLGPL, included in this distribution, + for details about redistributing fpGUI. + + 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. + + Description: + This unit contains some color conversion/mapping functions. +} + +unit fpg_ColorMapping; + +{$mode objfpc}{$H+} + +interface + +uses + Classes, fpg_base; + + // IN these functions the following definitions apply: + // Hue: 0 - 1535 + // red = 0 green = 512 blue = 1024 + // Saturation: 0 - 1 + // grey (no color) = 0 max color = 1 + // Value: 0 - 1 + // black =0 max brightness = 1 + +procedure RGBToHSV(C: TfpgColor; out Hue: longint; out Saturation, Value: double); +function HSVToRGB(const H: longint; const S, V: double): TfpgColor; + +implementation + +uses + Math; + +procedure RGBToHSV(C: TfpgColor; out Hue: longint; out Saturation, Value: double); +var + r, g, b: longint; + hi, lo: longint; + d: longint; + rgb: TRGBTriple; +begin + rgb := fpgColorToRGBTriple(C); + r := rgb.Red; + g := rgb.Green; + b := rgb.Blue; + hi := max(max(r, g), b); + lo := min(min(r, g), b); + d := hi - lo; + Value := hi / 256; + if d > 0 then + begin + if r = hi then + Hue := 256 * (g - b) div d + else if g = hi then + Hue := 512 + 256 * (b - r) div d + else + Hue := 1024 + 256 * (r - g) div d; + if Hue < 0 then + Hue := Hue + 1536; + end + else + Hue := 0; // doesn't matter (grey: Sat = 0) + + if hi > 0 then + Saturation := d / hi + else + Saturation := 0; // doesn't matter (black: Val = 0 +end; + +function HSVToRGB(const H: longint; const S, V: double): TfpgColor; +var + r, g, b: longint; + rgb: TRGBTriple; +begin + if (h < 0) or (h > 1535) or (S < 0) or (S > 1) or (V < 0) or (V > 1) then + begin + // Invalid value, use black + Result := 0; + exit; + end; + case h div 256 of + 0: + begin + r := 255; + g := h; + b := 0; + end; + 1: + begin + r := 511 - h; + g := 255; + b := 0; + end; + 2: + begin + r := 0; + g := 255; + b := h - 512; + end; + 3: + begin + r := 0; + g := 1023 - h; + b := 255; + end; + 4: + begin + r := h - 1024; + g := 0; + b := 255; + end; + 5: + begin + r := 255; + g := 0; + b := 1535 - h; + end; + end; + r := Trunc(V * (255 - S * (255 - r))); + g := Trunc(V * (255 - S * (255 - g))); + b := Trunc(V * (255 - S * (255 - b))); + rgb.Red := r; + rgb.Green := g; + rgb.Blue := b; + Result := RGBTripleTofpgColor(rgb); +end; + + +end. + |