DelphiDabbler Code Snippets Database

Snippet Selection

The following snippets from the Code Snippets Database have been requested.

AdjustImage Constants

Constants used to pass default values to the AdjustImage overloaded functions. Using these constants for the related function parameters means that the parameter will have no effect on the adjusted image.

const
  AINulBrightness = 0;        // "no change" brightness value
  AINulContrast = 0;          // "no change" contrast value
  AINulTint = 0;              // "no change" tint value
  AINulSaturation = 0;        // "no change" saturation value
  AINulGamma = 10000;         // "no change" gamma value

AdjustImage Types

Types for use with the AdjustImage overloaded functions. The types define valid ranges of values for various function parameters.

type
  TAIBrightness = -100..100;  // range of valid brightness values
  TAIContrast = -100..100;    // range of valid constrast values
  TAITint = -100..100;        // range of valid tint values
  TAISaturation = -100..100;  // range of valid saturation values
  TAIGamma = 2500..65000;     // range of valid gamma values

ContrastingColor

Returns a colour that contrasts well with a given background colour. The returned colour is either black or white.

function ContrastingColor(const BackgroundColor: Graphics.TColor):
  Graphics.TColor;
const
  HalfBrightness = ((0.3 * 255.0) + (0.59 * 255.0) + (0.11 * 255.0)) / 2.0;
var
  Brightness: Double;       // brightness value of colour
  RGB: Windows.TRGBTriple;  // RGB value of colour
begin
  RGB := ColorToRGBTriple(BackgroundColor); // converts system colours
  Brightness := (0.3 * RGB.rgbtRed)
    + (0.59 * RGB.rgbtGreen)
    + (0.11 * RGB.rgbtBlue);
  if Brightness > HalfBrightness then
    Result := Graphics.clblack
  else
    Result := Graphics.clwhite;
end;

CountColors

Counts and returns the number of unique RGB triples in the given TBitmap object. The bitmap must be in pf24Bit format.

function CountColors(const Bitmap: Graphics.TBitmap): Integer;
var
  Flags: array[Byte, Byte] of Classes.TBits;
  I, J, K: Integer;
  RowIn: PRGBTripleArray;
begin
  // Be sure bitmap is 24-bits/pixel
  Assert(Bitmap.PixelFormat = Graphics.pf24Bit);

  // Clear 2D array of TBits objects
  for J := 0 to 255 do
    for I := 0 to 255 do
      Flags[I, J] := nil;

  // Step through each scanline of image
  for J := 0 to Bitmap.Height - 1 do
  begin
    RowIn  := Bitmap.Scanline[J];
    for I := 0 to Bitmap.Width - 1 do
    begin
      with RowIn[I] do
      begin
        if not Assigned(Flags[rgbtRed, rgbtGreen]) then
        begin
          // Create 3D column when needed
          Flags[rgbtRed, rgbtGreen] := Classes.TBits.Create;
          Flags[rgbtRed, rgbtGreen].Size := 256;
        end;
        // Mark this R-G-B triple
        Flags[rgbtRed, rgbtGreen].Bits[rgbtBlue] := True;
      end;
    end;
  end;

  Result := 0;
  // Count and Free TBits objects
  for J := 0 to 255 do
  begin
    for I := 0 to 255 do
    begin
      if Assigned(Flags[I, J]) then
      begin
        for K := 0 to 255 do
          if Flags[I, J].Bits[K] then
            Inc(Result);
        Flags[I, J].Free;
      end;
    end;
  end;
end;

RGBQuadArray

A 256 element array of RGB quad values.

type
  TRGBQuadArray = packed array[Byte] of Windows.TRGBQuad;
  PRGBQuadArray = ^TRGBQuadArray;

RGBTripleArray

A 256 element array of RGB triple values.

type
  TRGBTripleArray = packed array[Byte] of Windows.TRGBTriple;
  PRGBTripleArray = ^TRGBTripleArray;

TColorRec

Cracker record that provides several different views of a TColor value and allows it to be assembled from its constituent parts.

type
  TColorRec = packed record
    case Integer of
      0:
        // TColor as Cardinal value
        (Color: Cardinal);
      1:
        // Array access to the 4 bytes of the TColor
        (Bytes: array[0..3] of Byte);
      2:
        // Red, Green, Blue and Alpha transparency values of the TColor
        (R, G, B, Alpha: Byte);
      3:
        // When SysFlag = $FF the TColor could be a system colour, in which case
        // SysColor identifies the system colour concerned.
        // WARNING: A colour with alpha transparency of $FF will also appear to
        // be a system colour, so use with care.
        (SysColor: Word; UnUsed, SysFlag: Byte);
  end;

TGreyScaleMethod

Enumeration of possible methods used when coverting a bitmap to greyscale.

type
  TGreyScaleMethod = (gsmLightness, gsmAverage, gsmLuminosity);

THSBColor

Record storing Hue, Saturation and Brightness colour space values.

type
  THSBColor = record
    Hue,                // hue (degrees)
    Saturation,         // saturation (percentage)
    Brightness: Double; // brightness (percentage)
  end;

View the whole database.

Go to the DelphiDabbler website.