The code above each line explained and heavily commented

unit SimpleLED;  // This is the unit name — all Pascal code is grouped in units

{$mode objfpc}{$H+}  
// {$mode objfpc} tells the compiler to use the Free Pascal "Object Pascal" mode.
// {$H+} means "use long strings" by default (AnsiString instead of ShortString).

interface  // Start of the INTERFACE section: declarations visible to other units

uses
  Classes,    // Basic component and streaming support (TComponent, etc.)
  SysUtils,   // Common system utilities (file ops, formatting, exceptions)
  Controls,   // Base visual control classes (TControl, TCustomControl, etc.)
  Graphics,   // Drawing classes/types (TCanvas, TColor, etc.)
  Math;       // Math functions like Min, Max, Sin, Cos, etc.

type
  // Enumeration that describes possible LED states.
  TLEDState = (ledOff, ledOn, ledWarning, ledError);

  // Our custom LED control class — inherits from TCustomControl (a blank drawable surface)
  TSimpleLED = class(TCustomControl)
  private
    FState: TLEDState;  // Stores the current LED state internally
    procedure SetState(AValue: TLEDState);  // Setter for State property
  protected
    procedure Paint; override;  // Called automatically when control needs to be redrawn
  public
    constructor Create(AOwner: TComponent); override; // Constructor, called when created
  published
    // Published section means these properties are visible in the Object Inspector
    property State: TLEDState
      read FState
      write SetState
      default ledOff;  // Default state is ledOff

    // The following properties are inherited from TCustomControl and
    // made visible in the Object Inspector for layout & visibility control
    property Align;
    property Anchors;
    property Visible;
    property Enabled;
  end;

procedure Register;  // Registers this component for the Lazarus IDE

implementation  // Start of IMPLEMENTATION section: actual code for the above declarations

// Procedure that tells Lazarus to add our control to the Component Palette
procedure Register;
begin
  RegisterComponents('Samples', [TSimpleLED]);
  // 'Samples' = tab name in the IDE's component palette
  // [TSimpleLED] = array of components to register
end;

// Constructor: called when the control is created
constructor TSimpleLED.Create(AOwner: TComponent);
begin
  inherited Create(AOwner); // Always call parent constructor first
  Width := 30;               // Default width
  Height := 30;              // Default height
  FState := ledOff;          // Start with the LED turned off
end;

// Setter for State property
procedure TSimpleLED.SetState(AValue: TLEDState);
begin
  if FState = AValue then Exit;  // No change → exit early
  FState := AValue;              // Store new state
  Invalidate;                    // Mark control as needing repaint → triggers Paint method
end;

// Paint method: draws the LED on the control's canvas
procedure TSimpleLED.Paint;
var
  LEDColor: TColor;  // Stores color based on state
  R: TRect;          // Rectangle defining client area
  CenterX, CenterY: Integer;  // Center position of LED
  Radius: Integer;            // Radius of LED circle
begin
  inherited Paint;  // Call parent Paint to do any default drawing (usually nothing)

  // Choose LED color based on the current state
  case FState of
    ledOff:     LEDColor := clGray;    // LED off = gray
    ledOn:      LEDColor := clLime;    // LED on = bright green
    ledWarning: LEDColor := clYellow;  // Warning = yellow
    ledError:   LEDColor := clRed;     // Error = red
  else
    LEDColor := clGray;                // Safety default
  end;

  // Fill background with control's Color property
  Canvas.Brush.Color := Self.Color;
  Canvas.FillRect(ClientRect);  // ClientRect = area of the control

  // Prepare variables for LED circle
  R := ClientRect;
  CenterX := (R.Left + R.Right) div 2;  // Horizontal center
  CenterY := (R.Top + R.Bottom) div 2;  // Vertical center
  Radius := Min(Width, Height) div 2 - 2;  // Radius with margin

  // Draw LED circle
  Canvas.Pen.Color := clBlack;     // Border color
  Canvas.Brush.Color := LEDColor;  // Fill color
  Canvas.Ellipse(
    CenterX - Radius, CenterY - Radius,  // Top-left
    CenterX + Radius, CenterY + Radius   // Bottom-right
  );

  // Draw small white "shine" circle for glossy effect
  Canvas.Brush.Color := clWhite;
  Canvas.Pen.Color := clWhite;
  Canvas.Ellipse(
    CenterX - Radius div 3, CenterY - Radius div 3,  // Small offset
    CenterX - Radius div 5, CenterY - Radius div 5
  );
end;

end.