Recent

Author Topic: [SOLVED] Defining custom color issues  (Read 315 times)

Aruna

  • Hero Member
  • *****
  • Posts: 574
[SOLVED] Defining custom color issues
« on: January 26, 2025, 03:08:25 pm »
Hi Everyone,

I am developing a resistor color code value calculator and decided to use custom-defined colors for Brown (TColor = $0015375F) and Orange (TColor = $001B5AF8) as a named value that can be reused elsewhere in your code.

The thing is if I inline the value directly into the array it works and when I try to use the named color it chokes.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Types;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     ComboBox1: TComboBox;
  17.     procedure ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  18.       ARect: TRect; State: TOwnerDrawState);
  19.     procedure FormCreate(Sender: TObject);
  20.   private
  21.  
  22.   public
  23.  
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. { TForm1 }
  34.  
  35. procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  36.   ARect: TRect; State: TOwnerDrawState);
  37.  
  38. const
  39.  
  40.   clBrown : TColor = $0015375F;  // Define custom color Brown
  41.   clOrange: TColor = $001B5AF8;  // Define custom color Orange
  42.  
  43.   ResistorColors: array[0..9] of TColor = (
  44.     clBlack, $0015375F, clRed, $001B5AF8, clYellow, // <---THIS WORKS NO ISSUES!
  45.     clGreen, clBlue, clPurple, clGray, clWhite
  46.   );
  47.  
  48.   ResistorColors: array[0..9] of TColor = (
  49.     clBlack, clBrown, clRed, clOrange, clYellow, // //<---THIS CHOKES. SCREENSHOT ATTACHED
  50.     clGreen, clBlue, clPurple, clGray, clWhite
  51.   );
  52.  
  53.  
  54. begin
  55.  
  56.   with (Control as TComboBox).Canvas do
  57.    begin
  58.     Brush.Style := bsSolid;
  59.     // Set the background color for the item
  60.     Brush.Color := ResistorColors[Index];
  61.    end;
  62.     ComboBox1.Canvas.FillRect(aRect);
  63.     Brush.Style := bsClear;
  64.     ComboBox1.Canvas.TextOut(aRect.Left + 2, aRect.Top + 2, ComboBox1.Items[Index]);
  65. end;
  66.  
  67. procedure TForm1.FormCreate(Sender: TObject);
  68. begin
  69.  
  70.   // Populate the combobox with resistor color band names
  71.   ComboBox1.Items.Add('Black - 0');
  72.   ComboBox1.Items.Add('Brown - 1');
  73.   ComboBox1.Items.Add('Red - 2');
  74.   ComboBox1.Items.Add('Orange - 3');
  75.   ComboBox1.Items.Add('Yellow - 4');
  76.   ComboBox1.Items.Add('Green - 5');
  77.   ComboBox1.Items.Add('Blue - 6');
  78.   ComboBox1.Items.Add('Violet - 7');
  79.   ComboBox1.Items.Add('Gray - 8');
  80.   ComboBox1.Items.Add('White - 9');
  81.  
  82.   // Set the combobox style to owner-draw
  83.   ComboBox1.Style := csOwnerDrawFixed;
  84.  
  85.  
  86. end;
  87.  
  88. end.
  89.  

The another thing I noticed is once an item is selectedt the combox  the background leaks, screenshot attached. How do I fix this?
« Last Edit: January 26, 2025, 03:44:23 pm by Aruna »

jamie

  • Hero Member
  • *****
  • Posts: 6801
Re: Defining custom color issues
« Reply #1 on: January 26, 2025, 03:22:39 pm »
Use a constant with name that reflects the value.

Const myorgange = $××××××××÷ etc
The only true wisdom is knowing you know nothing

Aruna

  • Hero Member
  • *****
  • Posts: 574
Re: Defining custom color issues
« Reply #2 on: January 26, 2025, 03:43:30 pm »
Use a constant with name that reflects the value.

Const myorgange = $××××××××÷ etc
Many many thanks that did the job :)

jamie

  • Hero Member
  • *****
  • Posts: 6801
Re: [SOLVED] Defining custom color issues
« Reply #3 on: January 26, 2025, 03:46:55 pm »
Ok. I don't know about your bleeding problem. Are u talking about one color overwriting a prior color band?
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018