.Color Conversions - RGB/BGR/Long

Category: Graphics

Date: 02-16-2022

Return to Index


 
'Color of a pixel is broken into red, green, blue elements.
'Each color element can be represented as &H00 - &HFF,
'or 0 - 256 decimal.
 
'Color elements are sometimes written in hex as follows,
'each with values ranging from 0-255.
red& = &HRR
blue& = &HGG
green& = &HBB
 
'The color elements can be combined into two different
'Long integer values - known as RGB and BGR.
 
'An RGB value has green as the lowest byte, blue as
'byte 2 and blue as byte 3.  BGR values are reversed.
'Byte 4 of both types is always zero.
 
'PowerBASIC offers the RGB() and BGR() functions to
'convert between the two Long value types.
 
'Primary Code:
'To return Long values from the red, green, blue elements:
rgbResult& = Rgb(red&, green&, blue&)    'RGB Long value
bgrResult& = Bgr(red&, green&, blue&)    'BGR Long value
 
'To convert between RGB/BGR types
bgrResult& = Bgr(rgbResult&)        'convert RGB Long to BGR Long
rgbResult& = Rgb(bgrResult&)        'convert BGR Long to RGB Long
 
 
'Compilable Example:  (Jose Includes)
'This example uses RGB to combine red, green, blue values of
'128 and uses them to gradient fill part of the dialog.  Whenever
'all 3 colors elements are the same, a shade of gray results
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg as Dword
 
Function PBMain() As Long
   Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
   Control Add Button, hDlg, 100,"Push", 50,10,100,20
   Dialog Show Modal hDlg Call DlgProc
End Function
 
CallBack Function DlgProc() As Long
   Local iRed&, iBlue&, iGreen&, iColor&, y&
   If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then
      iRed& = 128 : iBlue& = 128 : iGreen& = 128
      iColor& = Rgb(iRed&, iBlue&, iGreen&)
      Control Add Graphic, hDlg, 500,"", 0,50,200,150, %WS_Visible Or %WS_Border
      Graphic Attach hDlg, 500, Redraw
      For y& = 0 To 150 : Graphic Line (0, y&) - (200, y&), Rgb(y&, y&, y&) : Next
         Dialog Redraw hDlg
   End If
End Function
 
'gbs_00179
'Date: 03-10-2012


created by gbSnippets
http://www.garybeene.com/sw/gbsnippets.htm