Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 383
  • Last Modified:

Dimming a monitor/colours

Help.

Does anyone know of a way to dim the monitor through code.

0
dvk
Asked:
dvk
1 Solution
 
intheCommented:
hi,
do you mean the actual brightness of the moniter/display?
i would think that would be a hardware issue specific to your graphics card.my nvidea geforce2 has a slider (in color correction control panel)to do this but the software came specific for the card.
0
 
dvkAuthor Commented:
If it can be done, then yes through the brightness.  Or another way would be to have a component that would invert colours to simulate night time - is there something out there.

I have thougth about using colours, but this would be very time consuming and would prefer it if there was already a component that did this.

0
 
ginsonicCommented:
I can help you for an image brightness :(

The code fragment below converts colors between RGB (Red, Green, Blue) and
HLS/HBS (Hue, Lightness, Saturation/Hue, Brightness, Saturation).


MORE INFORMATION


/* Color Conversion Routines --

RGBtoHLS() takes a DWORD RGB value, translates it to HLS, and stores the
results in the global vars H, L, and S. HLStoRGB takes the current values of
H, L, and S and returns the equivalent value in an RGB DWORD. The vars H, L,
and S are only written to by:


   1. RGBtoHLS (initialization)
   2. The scroll bar handlers
A point of reference for the algorithms is Foley and Van Dam, "Fundamentals
of Interactive Computer Graphics," Pages 618-19. Their algorithm is in
floating point. CHART implements a less general (hardwired ranges) integral
algorithm.
There are potential round-off errors throughout this sample. ((0.5 + x)/y)
without floating point is phrased ((x + (y/2))/y), yielding a very small
round-off error. This makes many of the following divisions look strange. */

*************************************************************************)
const
  HLSMAX = 240;         (* H,L, and S vary over 0-HLSMAX *)
  RGBMAX = 255;         (* R,G, and B vary over 0-RGBMAX *)
                        (* HLSMAX BEST IF DIVISIBLE BY 6 *)
                        (* RGBMAX, HLSMAX must each fit in a byte. *)
(* Hue is undefined if Saturation is 0 (grey-scale) *)
(* This value determines where the Hue scrollbar is *)
(* initially set for achromatic colors *)
  UNDEFINED = HLSMAX*2 div 3;

function RGBtoHLS(RGBColor: DWORD): THLSVector;
var
   R, G, B: Word;              (* input RGB values *)
   cMax, cMin: Byte;           (* max and min RGB values *)
   Rdelta,Gdelta,Bdelta: Word; (* intermediate value: % of spread from max*)
begin
   (* get R, G, and B out of DWORD *)
   R := GetRValue(RGBColor);
   G := GetGValue(RGBColor);
   B := GetBValue(RGBColor);

   (* calculate lightness *)
   cMax := UMax( UMax(R,G), B);
   cMin := UMin( UMin(R,G), B);
   with Result do
   begin
      Luminance := ( ((cMax+cMin)*HLSMAX) + RGBMAX ) div (2*RGBMAX);

      if (cMax = cMin) then          (* r:=g:=b --> achromatic case *)
      begin
         Saturation := 0;                     (* saturation *)
         Hue := UNDEFINED;             (* hue *)
      end else
      begin                        (* chromatic case *)
         (* saturation *)
         if Luminance <= (HLSMAX div 2) then
            Saturation := ( ((cMax-cMin)*HLSMAX) + ((cMax+cMin) div 2) )
div  (cMax+cMin)
         else
            Saturation := ( ((cMax-cMin)*HLSMAX) + ((2*RGBMAX-cMax-cMin) div
2) )
                div  (2*RGBMAX-cMax-cMin);

         (* hue *)
         Rdelta := ( ((cMax-R)*(HLSMAX div 6)) + ((cMax-cMin) div 2) ) div
(cMax-cMin);
         Gdelta := ( ((cMax-G)*(HLSMAX div 6)) + ((cMax-cMin) div 2) ) div
(cMax-cMin);
         Bdelta := ( ((cMax-B)*(HLSMAX div 6)) + ((cMax-cMin) div 2) ) div
(cMax-cMin);

         if R = cMax then
            Hue := Bdelta - Gdelta
         else if G = cMax then
            Hue := (HLSMAX div 3) + Rdelta - Bdelta
         else (* B = cMax *)
            Hue := ((2*HLSMAX) div 3) + Gdelta - Rdelta;

         if Hue < 0 then
            Inc(Hue, HLSMAX);
         if Hue > HLSMAX then
            Dec(Hue, HLSMAX);
      end;
   end;
end;

function HueToRGB(n1,n2,hue: Word): Word;
(* utility routine for HLStoRGB *)
begin
   (* range check: note values passed add div subtract thirds of range *)
   if hue < 0 then
      Inc(hue, HLSMAX);

   if hue > HLSMAX then
      Dec(hue, HLSMAX);

   (* return r,g, or b value from this tridrant *)
   if hue < (HLSMAX div 6) then
      Result := ( n1 + (((n2-n1)*hue+(HLSMAX div 12)) div (HLSMAX div 6)) )
else
   if hue < (HLSMAX div 2) then
      Result := n2 else
   if hue < ((HLSMAX*2) div 3) then
      Result := ( n1 + (((n2-n1)*(((HLSMAX*2) div 3)-hue)+(HLSMAX div 12))
div (HLSMAX div 6)))
   else
      Result := n1;
end;

function HLStoRGB(Hue, Luminance, Saturation: Word): TColor;
var
   R, G, B: Word;              (* RGB component values *)
   Magic1, Magic2: Word;       (* calculated magic numbers (really!) *)
begin
   if Saturation = 0 then             (* achromatic case *)
   begin
      R :=(Luminance*RGBMAX) div HLSMAX;
      G := R;
      B := R;
      if Hue <> UNDEFINED then
      begin
         (* ERROR *)
      end
   end else
   begin                    (* chromatic case *)
      (* set up magic numbers *)
      if (Luminance <= (HLSMAX div 2)) then
         Magic2 := (Luminance*(HLSMAX + Saturation) + (HLSMAX div 2)) div
HLSMAX
      else
         Magic2 := Luminance + Saturation - ((Luminance*Saturation) +
(HLSMAX div 2)) div HLSMAX;
      Magic1 := 2*Luminance-Magic2;
      (* get RGB, change units from HLSMAX to RGBMAX *)
      R := (HueToRGB(Magic1,Magic2,Hue+(HLSMAX div 3))*RGBMAX +(HLSMAX div
2)) div HLSMAX;
      G := (HueToRGB(Magic1,Magic2,Hue)               *RGBMAX +(HLSMAX div
2)) div HLSMAX;
      B := (HueToRGB(Magic1,Magic2,Hue-(HLSMAX div 3))*RGBMAX +(HLSMAX div
2)) div HLSMAX;
   end;
   Result :=  RGB(R,G,B);
end;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
f15iafCommented:
I've used a ginsonic code:
Here is the example of what you need.
This program brightens the screen.

here is unit2

unit Unit2;
interface
uses windows,math,graphics;
const
 HLSMAX = 240;         (* H,L, and S vary over 0-HLSMAX *)
 RGBMAX = 255;         (* R,G, and B vary over 0-RGBMAX *)
                       (* HLSMAX BEST IF DIVISIBLE BY 6 *)
                       (* RGBMAX, HLSMAX must each fit in a byte. *)
(* Hue is undefined if Saturation is 0 (grey-scale) *)
(* This value determines where the Hue scrollbar is *)
(* initially set for achromatic colors *)
 UNDEFINED = HLSMAX*2 div 3;
type
        THLSVector=record
                Hue,Luminance,Saturation:Word;
        end;
function RGBtoHLS(RGBColor: DWORD): THLSVector;
function HLStoRGB(Hue, Luminance, Saturation: Word): TColor;
implementation
function RGBtoHLS(RGBColor: DWORD): THLSVector;
var
  R, G, B: Word;              (* input RGB values *)
  cMax, cMin: Byte;           (* max and min RGB values *)
  Rdelta,Gdelta,Bdelta: Word; (* intermediate value: % of spread from max*)
begin
  (* get R, G, and B out of DWORD *)
  R := GetRValue(RGBColor);
  G := GetGValue(RGBColor);
  B := GetBValue(RGBColor);

  (* calculate lightness *)
  cMax := Max( Max(R,G), B);
  cMin := Min( Min(R,G), B);
  with Result do
  begin
     Luminance := ( ((cMax+cMin)*HLSMAX) + RGBMAX ) div (2*RGBMAX);

     if (cMax = cMin) then          (* r:=g:=b --> achromatic case *)
     begin
        Saturation := 0;                     (* saturation *)
        Hue := UNDEFINED;             (* hue *)
     end else
     begin                        (* chromatic case *)
        (* saturation *)
        if Luminance <= (HLSMAX div 2) then
           Saturation := ( ((cMax-cMin)*HLSMAX) + ((cMax+cMin) div 2) )
div  (cMax+cMin)
        else
           Saturation := ( ((cMax-cMin)*HLSMAX) + ((2*RGBMAX-cMax-cMin) div
2) )
               div  (2*RGBMAX-cMax-cMin);

        (* hue *)
        Rdelta := ( ((cMax-R)*(HLSMAX div 6)) + ((cMax-cMin) div 2) ) div
(cMax-cMin);
        Gdelta := ( ((cMax-G)*(HLSMAX div 6)) + ((cMax-cMin) div 2) ) div
(cMax-cMin);
        Bdelta := ( ((cMax-B)*(HLSMAX div 6)) + ((cMax-cMin) div 2) ) div
(cMax-cMin);

        if R = cMax then
           Hue := Bdelta - Gdelta
        else if G = cMax then
           Hue := (HLSMAX div 3) + Rdelta - Bdelta
        else (* B = cMax *)
           Hue := ((2*HLSMAX) div 3) + Gdelta - Rdelta;

        if Hue < 0 then
           Inc(Hue, HLSMAX);
        if Hue > HLSMAX then
           Dec(Hue, HLSMAX);
     end;
  end;
end;

function HueToRGB(n1,n2,hue: Word): DWord;
(* utility routine for HLStoRGB *)
begin
  (* range check: note values passed add div subtract thirds of range *)
  if hue < 0 then
     Inc(hue, HLSMAX);

  if hue > HLSMAX then
     Dec(hue, HLSMAX);

  (* return r,g, or b value from this tridrant *)
  if hue < (HLSMAX div 6) then
     Result := ( n1 + (((n2-n1)*hue+(HLSMAX div 12)) div (HLSMAX div 6)) )
else
  if hue < (HLSMAX div 2) then
     Result := n2 else
  if hue < ((HLSMAX*2) div 3) then
     Result := ( n1 + (((n2-n1)*(((HLSMAX*2) div 3)-hue)+(HLSMAX div 12))
div (HLSMAX div 6)))
  else
     Result := n1;
end;

function HLStoRGB(Hue, Luminance, Saturation: Word): TColor;
var
  R, G, B: Word;              (* RGB component values *)
  Magic1, Magic2: Word;       (* calculated magic numbers (really!) *)
begin
  if Saturation = 0 then             (* achromatic case *)
  begin
     R :=(Luminance*RGBMAX) div HLSMAX;
     G := R;
     B := R;
     if Hue <> UNDEFINED then
     begin
        (* ERROR *)
     end
  end else
  begin                    (* chromatic case *)
     (* set up magic numbers *)
     if (Luminance <= (HLSMAX div 2)) then
        Magic2 := (Luminance*(HLSMAX + Saturation) + (HLSMAX div 2)) div
HLSMAX
     else
        Magic2 := Luminance + Saturation - ((Luminance*Saturation) +
(HLSMAX div 2)) div HLSMAX;
     Magic1 := 2*Luminance-Magic2;
     (* get RGB, change units from HLSMAX to RGBMAX *)
     R := (HueToRGB(Magic1,Magic2,Hue+(HLSMAX div 3))*RGBMAX +(HLSMAX div
2)) div HLSMAX;
     G := (HueToRGB(Magic1,Magic2,Hue)               *RGBMAX +(HLSMAX div
2)) div HLSMAX;
     B := (HueToRGB(Magic1,Magic2,Hue-(HLSMAX div 3))*RGBMAX +(HLSMAX div
2)) div HLSMAX;
  end;
  Result :=  RGB(R,G,B);
end;

end.
 

Here is the main unit:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);stdcall;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}
uses unit2;
procedure TForm1.Button1Click(Sender: TObject);stdcall;
var
        elem:array [1..28]of integer;
        col:array[1..28]of dword;
        hls:Thlsvector;
        tempr:dword;
        lm:integer;
        i:integer;
begin
        for i:=1 to 28 do
        begin
                tempr:=getsyscolor(i);
                elem[i]:=i;
                hls:=rgbtohls(tempr);
                lm:=hls.Luminance;
                lm:=lm+5;
                if(lm>240)then
                lm:=240;
                if(lm<0)then
                lm:=0;
                hls.Luminance:=lm;
                tempr:=hlstorgb(hls.Hue,hls.Luminance,hls.Saturation);
                col[i]:=tempr;
        end;
        setsyscolors(28,elem,col);
end;

end.
0
 
f15iafCommented:
if you want to dim monitor then instead of making lm:=lm+5
lm:=lm-5;
0
 
dvkAuthor Commented:
Absolutely marvellous, with a slight alteration for restoring back to default, it worked a treat.

Thanx a million.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now