Solved

Dimming a monitor/colours

Posted on 2001-06-06
6
366 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 3

Accepted Solution

by:
f15iaf earned 100 total points
Comment Utility
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
Comment Utility
if you want to dim monitor then instead of making lm:=lm+5
lm:=lm-5;
0
 

Author Comment

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

Thanx a million.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now