Transform labels' backcolor to grayscale

Topic Specific Tutorials and Tips.

Moderator: Rathinagiri

Post Reply
User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil
Has thanked: 100 times
Been thanked: 182 times

Transform labels' backcolor to grayscale

Post by Pablo César » Fri Dec 19, 2014 4:17 pm

When we need to become Enabled to .F. for LABELS, BackColor doens't change ! :o

I can force to gray color, but there are many different tones of gray and depending what color is that (bright or dark color). Then I should use accordingly to the right tone of gray. That's the problem. :(

Upto till now...

Because I found a way, not only with backcolor's labels also with any colours. A simple solution I fund with this:

Code: Select all

Function ChngToGray(aColor)
Local nGray:=(aColor[1]+aColor[2]+aColor[3])/3
Return {nGray,nGray,nGray}
Of course with help of internet...

See this demo:

Code: Select all

#include "hmg.ch"

Function Main
DEFINE WINDOW Form_1 ;
	AT 0,0 ;
	WIDTH 230 HEIGHT 130 ;
	TITLE "HMG Demo" ;
	MAIN ;
	FONT "Arial" SIZE 10 BACKCOLOR YELLOW

	DEFINE LABEL Label_9
        ROW    20
        COL    24
        WIDTH  90
        HEIGHT 20
        VALUE "Test"
        FONTNAME "Arial"
        FONTSIZE 9
        BACKCOLOR BLUE
        FONTCOLOR Nil
    END LABEL
	
    DEFINE BUTTON Button_3
        ROW    70
        COL    103
        WIDTH  100
        HEIGHT 28
        ACTION {|| Form_1.Label_9.BackColor := ChngToGray(Form_1.Label_9.BackColor)}
        CAPTION "Enabled Label_9"
        FONTNAME "Arial"
        FONTSIZE 9
        TOOLTIP ""
    END BUTTON
      
END WINDOW
CENTER WINDOW Form_1
ACTIVATE WINDOW Form_1
Return Nil

Function ChngToGray(aColor)
Local nGray:=(aColor[1]+aColor[2]+aColor[3])/3
Return {nGray,nGray,nGray}
Anyway if you want to disable any control, you still will need to change it, as follows:

<WindowName>.<ControlName>.Enabled := .F.

I hope, all of you enjoy it ! :D
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein

User avatar
srvet_claudio
Posts: 2044
Joined: Thu Feb 25, 2010 8:43 pm
Location: Uruguay
Has thanked: 35 times
Been thanked: 146 times
Contact:

Post by srvet_claudio » Fri Dec 19, 2014 7:15 pm

Pablo,
this is the best algorithm (C code):

Code: Select all

#define bt_RGB_TO_GRAY(R,G,B) (INT)((FLOAT)R * 0.299 + (FLOAT)G * 0.587 + (FLOAT)B * 0.114)
Is very easy the adaptation to HB.
Best regards.
Dr. Claudio Soto
(from Uruguay)
http://srvet.blogspot.com

User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil
Has thanked: 100 times
Been thanked: 182 times

Post by Pablo César » Fri Dec 19, 2014 7:37 pm

Thank you Claudio ! :D

Your algorithm is really better because I have adapted and compared.

Code: Select all

Function ChngToGray(aColor,nGrayLevel)
Local nGray := ( (aColor[1] * 0.299) + (aColor[2] * 0.587) + (aColor[3] * 0.114) )

HB_DEFAULT( @nGrayLevel, 0.0 )

If nGrayLevel > 1.0
   nGrayLevel := 1.0
Endif
Return {aColor[1] + (nGray - aColor[1]) * nGrayLevel, aColor[2] + (nGray - aColor[2]) * nGrayLevel, aColor[3] + (nGray - aColor[3]) * nGrayLevel}
Nice one ! :D

I improved my function similar to BT. :)
I did not see this instruction in BT. :oops:

But please Claudio, tell me: is it right Label's BackColor do not becoming gray when at Enabled := .F. ?
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein

User avatar
srvet_claudio
Posts: 2044
Joined: Thu Feb 25, 2010 8:43 pm
Location: Uruguay
Has thanked: 35 times
Been thanked: 146 times
Contact:

Post by srvet_claudio » Fri Dec 19, 2014 11:26 pm

Pablo César wrote: But please Claudio, tell me: is it right Label's BackColor do not becoming gray when at Enabled := .F. ?
Pablo, this works fine for my (Win 7):

Code: Select all

#include "hmg.ch"

Function Main

	DEFINE WINDOW Form_Main ;
		AT 0,0 ;
		WIDTH 640 HEIGHT 480 ;
		TITLE 'Main Window' ;
		MAIN 

		@ 200,250 LABEL Label_1 ;
		WIDTH 150 HEIGHT 40 ;
		VALUE 'Click Me !' ;
		ACTION MsgInfo('Label Clicked!!!') ;
		FONT 'Arial' SIZE 24  CENTERALIGN

		@ 10,10 LABEL Label_2 ;
		AUTOSIZE ;
		VALUE '...' ;
		ACTION msginfo('test')

	END WINDOW

   
	Form_Main.Label_2.Value := 'Hello All, This Is An AutoSIzable Label!!!'

   Form_Main.Label_1.Enabled := .F.
   Form_Main.Label_2.Enabled := .F.

	CENTER WINDOW Form_Main

	ACTIVATE WINDOW Form_Main

Return 
Best regards.
Dr. Claudio Soto
(from Uruguay)
http://srvet.blogspot.com

User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil
Has thanked: 100 times
Been thanked: 182 times

Post by Pablo César » Fri Dec 19, 2014 11:59 pm

Sorry Claudio for me insist, but probably you do not understood what I said.

The problem is in BlackColor of Label. See this example:

Code: Select all

#include "hmg.ch"

Function Main
DEFINE WINDOW Form_1 ;
	AT 0,0 ;
	WIDTH 230 HEIGHT 150 ;
	TITLE "HMG Demo" ;
	MAIN ;
	FONT "Arial" SIZE 10 BACKCOLOR YELLOW

	DEFINE LABEL Label_9
        ROW    20
        COL    24
        WIDTH  90
        HEIGHT 20
        VALUE "Test"
        FONTNAME "Arial"
        FONTSIZE 9
        BACKCOLOR BLUE
        FONTCOLOR Nil
    END LABEL
	
    DEFINE BUTTON Button_3
        ROW    70
        COL    10
        WIDTH  200
        HEIGHT 28
        ACTION {|| Form_1.Label_9.Enabled := .F.}
        CAPTION "Enabled to .F. for Label_9"
        FONTNAME "Arial"
        FONTSIZE 9
        TOOLTIP ""
    END BUTTON
      
END WINDOW
CENTER WINDOW Form_1
ACTIVATE WINDOW Form_1
Return Nil
As you can see below, the blue color is still blue:
Screen1.png
Screen1.png (4.39 KiB) Viewed 2030 times
I have attached my executable file. I do not believe is a bug in my HMG version.
Demo2.rar
(1.05 MiB) Downloaded 201 times
Or it would be at my OS ? :? (I do not use Aero)
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein

User avatar
srvet_claudio
Posts: 2044
Joined: Thu Feb 25, 2010 8:43 pm
Location: Uruguay
Has thanked: 35 times
Been thanked: 146 times
Contact:

Post by srvet_claudio » Sat Dec 20, 2014 1:02 am

Pablo César wrote:Or it would be at my OS ? (I do not use Aero)
Ok, I understand now your question.
Windows set to gray only the text, see this:

Code: Select all

#include "hmg.ch"

Function Main
DEFINE WINDOW Form_1 ;
   AT 0,0 ;
   WIDTH 230 HEIGHT 150 ;
   TITLE "HMG Demo" ;
   MAIN ;
   FONT "Arial" SIZE 10 BACKCOLOR YELLOW

   aLabelBackColor := GREEN

   DEFINE LABEL Label_9
        ROW    20
        COL    24
        WIDTH  90
        HEIGHT 20
        VALUE "Test"
        FONTNAME "Arial"
        FONTSIZE 9
        BACKCOLOR aLabelBackColor
        FONTCOLOR Nil
    END LABEL
   
    DEFINE BUTTON Button_3
        ROW    70
        COL    10
        WIDTH  200
        HEIGHT 28
        ACTION {|| Form_1.Label_9.Enabled := .NOT. Form_1.Label_9.Enabled, SetControlBackColorGray ( "Label_9", "Form_1", aLabelBackColor ) }
        CAPTION "Enabled/Disable Label_9"
        FONTNAME "Arial"
        FONTSIZE 9
        TOOLTIP ""
    END BUTTON
      
END WINDOW
CENTER WINDOW Form_1
ACTIVATE WINDOW Form_1
Return Nil


PROCEDURE SetControlBackColorGray ( cControlName, cParentName, aBackColor )
LOCAL nGray
  IF _IsControlEnabled ( cControlName, cParentName ) == .T.
      SetProperty ( cParentName, cControlName, "BackColor", aBackColor)
  ELSE
     nGray := INT ( (aBackColor[1] * 0.299) + (aBackColor[2] * 0.587) + (aBackColor[3] * 0.114) )
     SetProperty ( cParentName, cControlName, "BackColor", { nGray, nGray, nGray } )
  ENDIF
RETURN
Best regards.
Dr. Claudio Soto
(from Uruguay)
http://srvet.blogspot.com

User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil
Has thanked: 100 times
Been thanked: 182 times

Post by Pablo César » Sat Dec 20, 2014 2:04 am

Thank you Claudio for your clarification.

But making tests with FontColor:=BLACK and BackColor:={0,0,128} (dark blue), I got following results:
Screen1.png
Screen1.png (15.54 KiB) Viewed 2016 times
With your last code ( nGray := INT ( (aBackColor[1] * 0.299) + (aBackColor[2] * 0.587) + (aBackColor[3] * 0.114) ) ):
Screen3.png
Screen3.png (14.62 KiB) Viewed 2016 times
With my first example ( nGray:=(aColor[1]+aColor[2]+aColor[3])/3 ):
Screen2.png
Screen2.png (14.62 KiB) Viewed 2016 times
I know that colors BLACK with DARK_BLUE is no ideal for editing source. But if I give chance to user for changing, I need to do it in better results.
It's strange to see the difference and I made many other combinations, perhaps I saw this formula INT ( (aBackColor[1] * 0.299) + (aBackColor[2] * 0.587) + (aBackColor[3] * 0.114) ) ) in many C codes but maybe for images ? I don't know.
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein

Post Reply