Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Dimming a monitor/colours

Posted on 2001-06-06
6
369 Views
Last Modified: 2010-04-06
Help.

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

0
Comment
Question by:dvk
6 Comments
 
LVL 17

Expert Comment

by:inthe
ID: 6158878
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
 

Author Comment

by:dvk
ID: 6159650
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
 
LVL 9

Expert Comment

by:ginsonic
ID: 6162370
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 3

Accepted Solution

by:
f15iaf earned 100 total points
ID: 6165164
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
 
LVL 3

Expert Comment

by:f15iaf
ID: 6166845
if you want to dim monitor then instead of making lm:=lm+5
lm:=lm-5;
0
 

Author Comment

by:dvk
ID: 6167074
Absolutely marvellous, with a slight alteration for restoring back to default, it worked a treat.

Thanx a million.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question