Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I get the RGB and CMYK values of a TColor?

Delphi Imaging

How do I get the RGB and CMYK values of a TColor?

by  djjd47130  Posted    (Edited  )
I have built an object specifically to provide all the RGB and CMYK values together. It's designed to be used on components as a published property to be shown in the Object Inspector. It breaks down Red, Green, Blue, Cyan, Magenta, Yellow, and Black values, as well as the original TColor.

Code was extracted from a larger library of mine.

Code:
[code]
[navy][i]// for automatic syntax highlighting see faq102-6487 
[/i][/navy][navy][i]////////////////////////////////////////////////////////////////////////////////
[/i][/navy][navy][i]//  Delphi 7 Unit "JDGraphics.pas"
[/i][/navy][navy][i]//    JD's Custom Graphics Library
[/i][/navy][navy][i]//  Classes:
[/i][/navy][navy][i]//    TJDColor: TPersistent - Custom TColor wrapper with RGB and CMYK properties
[/i][/navy][navy][i]////////////////////////////////////////////////////////////////////////////////
[/i][/navy]
[b]unit[/b] JDGraphics;

[b]interface[/b]
 
[b]uses[/b]
  Classes, Controls, SysUtils, Windows, StdCtrls, ExtCtrls, Graphics, StrUtils,
  JPEG, Messages, Variants, Forms, Dialogs, Buttons, ComCtrls, ImgList;

[navy][i]////////////////////////////////////////////////////////////////////////////////
[/i][/navy][navy][i]//                                TJDColor
[/i][/navy][navy][i]//             Custom color wrapper providing RGB and CMYK values
[/i][/navy][navy][i]////////////////////////////////////////////////////////////////////////////////
[/i][/navy]
[b]type[/b]
  TJDColor = [b]class[/b];

  TColEvent = [b]procedure[/b](Sender: TObject) [b]of[/b] [b]object[/b];

  TJDColor = [b]class[/b](TPersistent)
    [b]private[/b]
      FColor: TColor;
      fEvent: TColEvent;
      [b]procedure[/b] SetColor(Value: TColor);
      [b]function[/b] GetRed: Integer;
      [b]function[/b] GetGreen: Integer;
      [b]function[/b] GetBlue: Integer;
      [b]procedure[/b] SetRed(Value: Integer);
      [b]procedure[/b] SetGreen(Value: Integer);
      [b]procedure[/b] SetBlue(Value: Integer);
      [b]function[/b] GetCyan: Integer;
      [b]function[/b] GetMagenta: Integer;
      [b]function[/b] GetYellow: Integer;
      [b]function[/b] GetBlack: Integer;
      [b]procedure[/b] SetCyan(Value: Integer);
      [b]procedure[/b] SetMagenta(Value: Integer);
      [b]procedure[/b] SetYellow(Value: Integer);
      [b]procedure[/b] SetBlack(Value: Integer);
      [b]procedure[/b] RaiseEvent;
    [b]public[/b]
      [b]constructor[/b] Create;
      [b]property[/b] Red: Integer [b]read[/b] GetRed [b]write[/b] SetRed;
      [b]property[/b] Green: Integer [b]read[/b] GetGreen [b]write[/b] SetGreen;
      [b]property[/b] Blue: Integer [b]read[/b] GetBlue [b]write[/b] SetBlue;
      [b]property[/b] Cyan: Integer [b]read[/b] GetCyan [b]write[/b] SetCyan;
      [b]property[/b] Magenta: Integer [b]read[/b] GetMagenta [b]write[/b] SetMagenta;
      [b]property[/b] Yellow: Integer [b]read[/b] GetYellow [b]write[/b] SetYellow;
      [b]property[/b] Black: Integer [b]read[/b] GetBlack [b]write[/b] SetBlack;
    [b]published[/b]
      [b]property[/b] Color: TColor [b]read[/b] FColor [b]write[/b] SetColor;
      [b]property[/b] OnEvent: TColEvent [b]read[/b] fEvent [b]write[/b] fEvent;
  [b]end[/b];



[b]implementation[/b]



[navy][i]////////////////////////////////////////////////////////////////////////////////
[/i][/navy][navy][i]//                                 TJDColor
[/i][/navy][navy][i]//     Stores TColor and provides read/write of RGB and CMYK color values
[/i][/navy][navy][i]////////////////////////////////////////////////////////////////////////////////
[/i][/navy]
[b]constructor[/b] TJDColor.Create;
[b]begin[/b]
  Self.Color:= clBlack;
  Self.RaiseEvent;
[b]end[/b];

[b]procedure[/b] TJDColor.SetColor(Value: TColor);
[b]begin[/b]
  Self.FColor:= Value;
  Self.RaiseEvent;
[b]end[/b];

[b]function[/b] TJDColor.GetRed: Integer;
[b]begin[/b]
  Result:= GetRValue(FColor);
[b]end[/b];

[b]function[/b] TJDColor.GetGreen: Integer;
[b]begin[/b]
  Result:= GetGValue(FColor);
[b]end[/b];

[b]function[/b] TJDColor.GetBlue: Integer;
[b]begin[/b]
  Result:= GetBValue(FColor);
[b]end[/b];

[b]procedure[/b] TJDColor.SetRed(Value: Integer);
[b]begin[/b]
  Self.FColor:= RGB(Value, GetGreen, GetBlue);
  RaiseEvent;
[b]end[/b];

[b]procedure[/b] TJDColor.SetGreen(Value: Integer);
[b]begin[/b]
  Self.FColor:= RGB(GetRed, Value, GetBlue);  
  RaiseEvent;
[b]end[/b];

[b]procedure[/b] TJDColor.SetBlue(Value: Integer);
[b]begin[/b]
  Self.FColor:= RGB(GetRed, GetGreen, Value);  
  RaiseEvent;
[b]end[/b];

[b]function[/b] TJDColor.GetCyan: Integer;
[b]begin[/b]
  Result:= GetCValue(FColor);
[b]end[/b];

[b]function[/b] TJDColor.GetMagenta: Integer;
[b]begin[/b]
  Result:= GetMValue(FColor);
[b]end[/b];

[b]function[/b] TJDColor.GetYellow: Integer;
[b]begin[/b]
  Result:= GetYValue(FColor);
[b]end[/b];

[b]function[/b] TJDColor.GetBlack: Integer;
[b]begin[/b]
  Result:= GetBValue(FColor);
[b]end[/b];

[b]procedure[/b] TJDColor.SetCyan(Value: Integer);
[b]begin[/b]
  Self.FColor:= CMYK(Value, GetMagenta, GetYellow, GetBlack); 
  RaiseEvent;
[b]end[/b];

[b]procedure[/b] TJDColor.SetMagenta(Value: Integer);
[b]begin[/b]
  Self.FColor:= CMYK(GetCyan, Value, GetYellow, GetBlack);    
  RaiseEvent;
[b]end[/b];

[b]procedure[/b] TJDColor.SetYellow(Value: Integer);
[b]begin[/b]
  Self.FColor:= CMYK(GetCyan, GetMagenta, Value, GetBlack);  
  RaiseEvent;
[b]end[/b];

[b]procedure[/b] TJDColor.SetBlack(Value: Integer);
[b]begin[/b]
  Self.FColor:= CMYK(GetCyan, GetMagenta, GetYellow, Value); 
  RaiseEvent;
[b]end[/b];

[b]procedure[/b] TJDColor.RaiseEvent; [navy][i]//Postback for components
[/i][/navy][b]begin[/b]
  [b]if[/b] assigned(Self.fEvent) [b]then[/b] [b]begin[/b]
    Self.fEvent(Self);
  [b]end[/b];
[b]end[/b];

[b]end[/b].

[/code]

Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top