News: SPECIAL OFFER: EZGUI Classic (4.0) until May 31, 2013 only $29 !

TO REGISTER on Forum, contact tech support: http://cwsof.com/support.html
 
Pages: [1]   Go Down
  Print  
Author Topic: Averaging Colour  (Read 314 times)
Brian Reynolds
PreRelease 4.5
Sr. Member
*
Posts: 304


View Profile
« on: June 03, 2012, 07:55:36 PM »

I am having problems with EZ_AverageColor
In each of three cases I am trying to get an average between 1 and 9, 2 and 10, 4 and 12.
If I put the following code before EZ_StartDraw, I only get a pale image of the default form background color.
If I put the code after EZ_StartDraw, the red works okay, giving a good average of the two Reds, but the blue and green both turn out very pale.

Code:
   ' MAKE NEW RED ===========================
      r1&=EZ_ColorVal(4,0)
      r2&=EZ_ColorVal(12,0)
      Newr& = EZ_AverageColor(r1&, r2&)
      EZ_DefColorL 53, Newr&
      msgbox str$(r1&) + "    " + str$(r2&) + "    " + str$(Newr&)
   ' MAKE NEW BLUE ==========================
      b1&=EZ_ColorVal(1,0)
      b2&=EZ_ColorVal(9,0)
      Newb& = EZ_AverageColor(b1&, b2&)
      EZ_DefColorL 54, Newb&
   ' MAKE NEW GREEN  =======================
      g1&=EZ_ColorVal(2,0)
      g2&=EZ_ColorVal(10,0)
      Newg& = EZ_AverageColor(g1&, g2&)
      EZ_DefColorL 55, Newg&
Is there any way to overcome the problem?
Or have I got something wrong?
Logged
Chris Boss
Administrator
Hero Member
*****
Posts: 1812


View Profile
« Reply #1 on: June 03, 2012, 08:29:47 PM »

You likely forgot to make sure all the default colors were defined. While EZGUI does have a definition for all the colors from 0 to 31, they are not actually defined until you call EZ_Color once with that color index used. EZGUI does this to save on resources, since each defined color requires one API brush handle internally, so why create a brush if it may not be used. When you call EZ_Color, EZGUI recognizes if a color has been defined yet, and if not and it is a default color, it then creates it.

Your EZ_ColorVal calls may fail to get the default color expected and EZGUI returns a substitue gray color (a system color instead).

Here is an example app which demonstrates using EZ_AverageColor and it does work.

Code:
' Portions: Copyright Christopher R. Boss, 2003 to 2011 , All Rights Reserved !
' Registered EZGUI 5.0 users may use this code Royalty Free !

'
' ======================================
' [PROTECTED CODE]         Do NOT Edit !
' ======================================
'
#COMPILE EXE
#DIM ALL        '   This is helpful to prevent errors in coding
#INCLUDE "C:\ezgui45beta\includes\ezgui50.inc"                          ' EZGUI Include file for Declares
'
DECLARE FUNCTION Main_Initialize(BYVAL VerNum&) AS LONG
DECLARE SUB EZ_FORM1_Display(BYVAL FParent$)
DECLARE SUB EZ_FORM1_Design()
DECLARE SUB EZ_FORM1_ParseEvents(CID&, CMsg&, CVal&, Cancel&)
DECLARE SUB FORM1_Events(CID&, CMsg&, CVal&, Cancel&)
'
%FORM1_BUTTON1            = 100
%FORM1_LABEL1             = 105
%FORM1_LABEL2             = 110
%FORM1_LABEL3             = 115
%FORM1_CMPLABEL1          = 120
%FORM1_CMPLABEL2          = 125
%FORM1_CMPLABEL3          = 130
%FORM1_CMPLABEL4          = 135
%FORM1_CMPLABEL5          = 140
%FORM1_CMPLABEL6          = 145
'
DECLARE SUB FORM1_BUTTON1_Events(MyID&, CMsg&, CVal&, Cancel&)
'
#INCLUDE "C:\ezgui45beta\includes\ezwmain50.inc"                          ' EZGUI Include file for WinMain
'
SUB EZ_Main(VerNum&)     ' (PROTECTED)
     EZ_Reg %EZ_CUSTID,%EZ_REGNUM
     EZ_DefImageFolder "Graphics"
     EZ_AllowCommandEvents  0
     EZ_DefFont 6, "Arial", 10, "V"
     EZ_DefFont 7, "Courier New", 10, "F"
     EZ_DefFont 8, "Times New Roman", 10, "V"
     EZ_DefFont 9, "Modern", 10, "V"
     EZ_DefSystemColor 32, 4
     EZ_DefSystemColor 33, 5
     EZ_DefSystemColor 34, 15
     EZ_DefSystemColor 35, 24
     EZ_DefColorL 36, &H00B96FFF
     EZ_DefColorL 37, &H0014AB9F
     EZ_DefColorL 38, &H0047A7FF
     EZ_DefColorL 39, &H00D2AACF
     EZ_DefColorL 40, &H001CD5E3
     EZ_DefColorL 41, &H00BC8943
     EZ_DefColorL 42, &H006C6AB7
     EZ_DefColorL 43, &H00DD4489
     IF Main_Initialize(VerNum&) THEN
          EZ_FORM1_Display ""
     END IF
END SUB
'
SUB EZ_DesignWindow(FormName$)     ' (PROTECTED)
     SELECT CASE FormName$
          CASE "FORM1"
               EZ_FORM1_Design
          CASE ELSE
     END SELECT
END SUB
'
SUB EZ_Events(FormName$, CID&, CMsg&, CVal&, Cancel&)     ' (PROTECTED)
     SELECT CASE FormName$
          CASE "FORM1"
               EZ_FORM1_ParseEvents CID&, CMsg&, CVal&, Cancel&
          CASE ELSE
     END SELECT
END SUB
'
' ======================================
' [USER ACCESSABLE CODE]  You may Edit !
' ======================================
'
FUNCTION Main_Initialize(BYVAL VerNum&) AS LONG
     LOCAL RV&
     RV&=1
     FUNCTION=RV&
END FUNCTION
'
'<<BEGINFORM>> "FORM1"
'
' ======================================
' [PROTECTED CODE]         Do NOT Edit !
' ======================================
'
SUB EZ_FORM1_Display(BYVAL FParent$)     ' (PROTECTED)
     EZ_Color -1, -1
     EZ_Form "FORM1", FParent$, "Test color averaging", 0, 0, 109.5, 14, "C"
END SUB
'
SUB EZ_FORM1_Design()     ' (PROTECTED)
     LOCAL CText$
     EZ_Color-1,-1
     EZ_UseFont 4
     EZ_Button %FORM1_BUTTON1, 3.125, 2.875, 19.875, 4.8125, "Average Colors Now", "T"
     EZ_Color 0, 4
     EZ_UseFont 4
     EZ_Label %FORM1_LABEL1, 33.625, 3.4375, 14.125, 6.1875, "", "CF"
     EZ_Color 0, 1
     EZ_UseFont 4
     EZ_Label %FORM1_LABEL2, 53.25, 3.4375, 14.125, 6.1875, "", "CF"
     EZ_Color 0, 2
     EZ_UseFont 4
     EZ_Label %FORM1_LABEL3, 72.875, 3.4375, 14.125, 6.1875, "", "CF"
     EZ_Color 0, 4
     EZ_UseFont 4
     EZ_Label %FORM1_CMPLABEL1, 33.625, .4375, 14.125, 2.6875, "", "CF"
     EZ_Color 0, 1
     EZ_UseFont 4
     EZ_Label %FORM1_CMPLABEL2, 53.25, .4375, 14.125, 2.6875, "", "CF"
     EZ_Color 0, 2
     EZ_UseFont 4
     EZ_Label %FORM1_CMPLABEL3, 72.875, .4375, 14.125, 2.6875, "", "CF"
     EZ_Color 0, 12
     EZ_UseFont 4
     EZ_Label %FORM1_CMPLABEL4, 33.625, 9.9375, 14.125, 2.6875, "", "CF"
     EZ_Color 0, 9
     EZ_UseFont 4
     EZ_Label %FORM1_CMPLABEL5, 53.25, 9.9375, 14.125, 2.6875, "", "CF"
     EZ_Color 0, 10
     EZ_UseFont 4
     EZ_Label %FORM1_CMPLABEL6, 72.875, 9.9375, 14.125, 2.6875, "", "CF"
END SUB
'
SUB EZ_FORM1_ParseEvents(CID&, CMsg&, CVal&, Cancel&)     ' (PROTECTED)
     SELECT CASE CID&
          CASE %EZ_Window
               FORM1_Events CID&, CMsg&, CVal&, Cancel&
          CASE  %FORM1_BUTTON1
               FORM1_BUTTON1_Events CID&, CMsg&, CVal&, Cancel&
          CASE ELSE
               FORM1_Events CID&, CMsg&, CVal&, Cancel&
     END SELECT
END SUB
'
' ======================================
' [USER ACCESSABLE CODE]  You may Edit !
' ======================================
'
SUB FORM1_Events(CID&, CMsg&, CVal&, Cancel&)
     SELECT CASE CID&
          CASE %EZ_Window
               SELECT CASE CMsg&
                    CASE %EZ_Loading
                    CASE %EZ_Loaded
                    CASE %EZ_Started
                    CASE %EZ_Close
                    CASE ELSE
               END SELECT
          CASE ELSE
     END SELECT
END SUB
'
SUB FORM1_BUTTON1_Events( MyID&, CMsg&, CVal&, Cancel&)
     SELECT CASE CMsg&
          CASE %EZ_Click
            LOCAL r1&,r2&,Newr&,b1&,b2&,Newb&,g1&,g2&,Newg&, N&
            FOR N&=0 TO 31      ' make sure colors are defined by a call to EZ_Color just once
                EZ_Color 0,N&
            NEXT N&
            r1&=EZ_ColorVal(4,0)
            r2&=EZ_ColorVal(12,0)
            Newr& = EZ_AverageColor(r1&, r2&)
            EZ_DefColorL 53, Newr&
            EZ_SetColor "{ME}",%FORM1_LABEL1, 0, 53
            b1&=EZ_ColorVal(1,0)
            b2&=EZ_ColorVal(9,0)
            Newb& = EZ_AverageColor(b1&, b2&)
            EZ_DefColorL 54, Newb&
            EZ_SetColor "{ME}",%FORM1_LABEL2, 0, 54
            g1&=EZ_ColorVal(2,0)
            g2&=EZ_ColorVal(10,0)
            Newg& = EZ_AverageColor(g1&, g2&)
            EZ_DefColorL 55, Newg&
            EZ_SetColor "{ME}",%FORM1_LABEL3, 0, 55
          CASE ELSE
     END SELECT
END SUB
Logged
Brian Reynolds
PreRelease 4.5
Sr. Member
*
Posts: 304


View Profile
« Reply #2 on: June 03, 2012, 08:49:44 PM »

Thanks, Chris.
Works perfectly.
Logged
Pages: [1]   Go Up
  Print  
 
Jump to: