Recent

Author Topic: Progressbar color fade?  (Read 1807 times)

entrisity

  • New Member
  • *
  • Posts: 12
Progressbar color fade?
« on: September 20, 2018, 06:00:56 pm »
Hey guys,

I'm making a school project (game) and want to have a hp bar. I want it to be green if you have full health and go over to red if you have low hp (so it should fade/ have a gradient). I wanted to ask if there is any way to do that (I'm fine to download a package that support that kind of progress bar). I know that I can just do loads of if statements and the more i have the better the fade is but i want a other method that takes less work.

Thanks in advance!

Handoko

  • Hero Member
  • *****
  • Posts: 3547
  • My goal: build my own game engine using Lazarus
Re: Progressbar color fade?
« Reply #1 on: September 20, 2018, 07:38:10 pm »
There is no ready-to-use color fade progress bar. But you can easily create the effect. For example you can try the test.zip below:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Forms, Graphics, ExtCtrls, BGRAFlashProgressBar;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     BGRAFlashProgressBar1: TBGRAFlashProgressBar;
  16.     Timer1: TTimer;
  17.     procedure Timer1Timer(Sender: TObject);
  18.   end;
  19.  
  20. var
  21.   Form1: TForm1;
  22.  
  23. implementation
  24.  
  25. {$R *.lfm}
  26.  
  27. { TForm1 }
  28.  
  29. procedure TForm1.Timer1Timer(Sender: TObject);
  30. var
  31.   HP: Integer;
  32.   ValRed, ValGreen, ValBlue: Byte;
  33. begin
  34.  
  35.   HP := BGRAFlashProgressBar1.Value;
  36.   if HP  > 0 then
  37.     Dec(HP)
  38.   else
  39.     HP := BGRAFlashProgressBar1.MaxValue;
  40.   BGRAFlashProgressBar1.Value := HP;
  41.  
  42.   ValRed   := Round((1 - HP/BGRAFlashProgressBar1.MaxValue) * 255);
  43.   ValGreen := Round(HP / BGRAFlashProgressBar1.MaxValue * 255);
  44.   ValBlue  := Round(255 - abs(ValRed-ValGreen));
  45.   BGRAFlashProgressBar1.Color := RGBToColor(ValRed, ValGreen, ValBlue);
  46.  
  47. end;
  48.  
  49. end.

The code uses TBGRAFlashProgressBar from BGRAControl package:
http://wiki.freepascal.org/BGRAControls

You can manually install the package or you can install it using Online Package Manager:
Lazarus main menu > Package > Online Package Manager

entrisity

  • New Member
  • *
  • Posts: 12
Re: Progressbar color fade?
« Reply #2 on: September 20, 2018, 07:56:59 pm »
Hey, thanks for responding,

I'm kind of a newbie when it comes to open other projects. When im opening yours im getting following error message (see in picture). How do i fix that? :P
https://imgur.com/a/Hdd62yO

Handoko

  • Hero Member
  • *****
  • Posts: 3547
  • My goal: build my own game engine using Lazarus
Re: Progressbar color fade?
« Reply #3 on: September 20, 2018, 08:02:25 pm »
Just tested, I have no problem with the download and opening the project. Have you installed BGRAControl package properly?

The image you provided shows that you're opening the file directly from temporary folder. You should unzip all the files to a new location (maybe Desktop) first, then you open the project from there.

wp

  • Hero Member
  • *****
  • Posts: 7235
Re: Progressbar color fade?
« Reply #4 on: September 20, 2018, 09:57:18 pm »
[...] want to have a hp bar. I want it to be green if you have full health and go over to red if you have low hp (so it should fade/ have a gradient).
The Lazarus port of the JVCL library contains a TJvSpecialProgressbar which can be configured to display a continuous color gradient. Unfortunately the component is not completely self-contained and you must install the packages JvCoreLazR, JvCoreLazD, JvMMLazR, JvMMLazD - you can find them in the Online-Package-Manager as subitems of the JVCLLaz entry.

Or if you can live with a little modfied version you can try the one in the attachment which is ready to use - you only copy it into your project folder and create it and set its properties at run-time, just like in the demo project.
« Last Edit: September 20, 2018, 10:00:05 pm by wp »
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

sash

  • Sr. Member
  • ****
  • Posts: 353
Re: Progressbar color fade?
« Reply #5 on: September 21, 2018, 01:21:44 pm »
I know that I can just do loads of if statements and the more i have the better the fade is

You don't have to. For simple color transition use color blending / interpolation.
Code: Pascal  [Select][+][-]
  1. function ColorLerp(col1, col2 : TColor; factor : single {0.0 .. 1.0}) : TColor;
  2.  
  3.   function ByteLerp(val1, val2 : byte; factor : single) : byte;
  4.   begin
  5.     Result := Round( (val1 * factor) + ( (1 - factor) * val2) );
  6.   end;
  7. begin
  8.   Result := RGBToColor
  9.   (
  10.       ByteLerp(Red(col1), Red(col2), factor ),
  11.       ByteLerp(Green(col1), Green(col2), factor ),
  12.       ByteLerp(Blue(col1), Blue(col2), factor )
  13.   );
  14. end;
  15.  

You can create your own Class/Helper utility function based on provided demo form, where TShape is simply resized according to scale factor.
No ifs were used 8)
Lazarus 2.0.8 FPC 3.0.4 x86_64-linux-gtk2 @ Ubuntu 20.04 XFCE

 

TinyPortal © 2005-2018