Icone multi états

Partagez votre expérience de PureBasic avec les autres utilisateurs.
Avatar de l’utilisateur
microdevweb
Messages : 1798
Inscription : mer. 29/juin/2011 14:11
Localisation : Belgique

Icone multi états

Message par microdevweb »

Bonjour,

Pour une usage perso je me suis un petit code qui me crée une png à 4 états (Inactif, survolé, cocher, désactivé). On importe une png, chois la taille (le png est centré et le dernier est converti en Nb)

Note: code fait à la vas vite

Exemple:

Image

Code : Tout sélectionner

Enumeration 
    #MainForm
    #MainCanvas
    #BtImport
    #SpinSize
    #BtSave
EndEnumeration
EnableExplicit
UsePNGImageDecoder()
UsePNGImageEncoder()
Global gImg=-1
Global gDisableImg
Global gExportImg
Structure Col
    Color.i
    X.i
    Y.i
EndStructure
Global NewList myColor.Col()
Declare Draw()
Procedure CloseMainForm()
    End
EndProcedure
Procedure ConvertImage()
    Protected X,Y
    If IsImage(gDisableImg)
            FreeImage(gDisableImg)
        EndIf
        gDisableImg=CreateImage(#PB_Any,ImageWidth(gImg),ImageHeight(gImg),32,$FFFFFF)
        ClearList(myColor())
        StartDrawing(ImageOutput(gImg))
        For X=0 To ImageWidth(gImg)-1
            For Y=0 To ImageHeight(gImg)-1
                If  Point(X,Y)<>0
                    AddElement(myColor())
                    With myColor()
                        \X=X
                        \Y=Y
                        \Color=Point(X,Y)
                    EndWith
                EndIf
            Next
        Next
        StopDrawing()
        StartDrawing(ImageOutput(gDisableImg))
        Protected R,G,B,gray
        ForEach myColor()
            With myColor()
                R    = \Color & $ff
                G    = \Color >> 8 & $ff
                B    = \Color >> 16 & $ff
                gray = 0.2126*R + 0.7152*G + 0.0722*B
                Plot(\X, \Y, gray + gray << 8 + gray << 16)
            EndWith
        Next
        StopDrawing()
EndProcedure
Procedure EventBtImport()
    Protected file$
    file$=OpenFileRequester("Sélection d'une image","","PNG|*.png",0)
    If file$<>""
        gImg=LoadImage(#PB_Any,file$)
    EndIf
    Draw()
EndProcedure
Procedure EventSpinSize()
    Draw()
EndProcedure
Procedure Draw()
    Protected NbStage=4
    Protected Size=64
    Dim BgColor.d(3)
    BgColor(0)=$FFFFFFFF
    BgColor(1)=$00FFFF
    BgColor(2)=$578B2E
    BgColor(3)=$FFFFFFFF
    Protected N,X,Y,XI,YI
    Protected SizeImg=GetGadgetState(#SpinSize)
    If IsImage(gExportImg)<>0
        FreeImage(gExportImg)
    EndIf
    If gImg<>-1
        ConvertImage()
    EndIf
    gExportImg=CreateImage(#PB_Any,Size*NbStage,Size)
    StartDrawing(ImageOutput(gExportImg))
    DrawingMode(#PB_2DDrawing_Default)
    For N=1 To NbStage
        DrawingMode(#PB_2DDrawing_Default)
        If BgColor(N-1)>0
            Box(X,Y,Size,Size,BgColor(N-1))
        EndIf
        
        If gImg<>-1
            XI=X+(Size/2)-(SizeImg/2)
            YI=Y+(Size/2)-(SizeImg/2)
            DrawingMode(#PB_2DDrawing_AlphaClip)
            If N=NbStage
                DrawImage(ImageID(gDisableImg),XI,YI,SizeImg,SizeImg)
            Else
                DrawImage(ImageID(gImg),XI,YI,SizeImg,SizeImg)
            EndIf
        EndIf
        X+Size
    Next
    StopDrawing()
    Y=(GadgetHeight(#MainCanvas)/2)-(Size/2)
    X=(GadgetWidth(#MainCanvas)/2)-((Size*NbStage)/2)
    StartDrawing(CanvasOutput(#MainCanvas))
    Box(0,0,GadgetWidth(#MainCanvas),GadgetHeight(#MainCanvas),$FFFFFF)
    DrawingMode(#PB_2DDrawing_AlphaClip)
    DrawImage(ImageID(gExportImg),X,Y)
    StopDrawing()
EndProcedure
Procedure EventSave()
    Protected file$,img,X,Y
    file$=SaveFileRequester("Sauvegarde de l'image","","PNG|*.png",0)
    If file$<>""
        ClearList(myColor())
        StartDrawing(ImageOutput(gExportImg))
        For X=0 To ImageWidth(gExportImg)-1
            For Y=0 To ImageHeight(gExportImg)-1
                AddElement(myColor())
                With myColor()
                    \X=X
                    \Y=Y
                    If Point(X,Y)=$FFFFFF
                        \Color=RGBA(0, 0, 0, 0)
                    Else
                        \Color=Point(X,Y)
                    EndIf
                EndWith
            Next
        Next
        StopDrawing()
        If IsImage(img)<>0
            FreeImage(img)
        EndIf
        img=CreateImage(#PB_Any,ImageWidth(gExportImg),ImageHeight(gExportImg),32)
        StartDrawing(ImageOutput(img))
;         DrawingMode(#PB_2DDrawing_AlphaChannel)
;         Box(0,0,ImageWidth(gExportImg),ImageHeight(gExportImg),RGBA(0, 0, 0, 0))
;         DrawingMode(#PB_2DDrawing_Default)
        ForEach myColor()
            With myColor()
                If \Color=RGBA(0, 0, 0, 0)
                    DrawingMode(#PB_2DDrawing_AlphaChannel)
                Else
                    DrawingMode(#PB_2DDrawing_Default)
                EndIf
                Plot(\X,\Y,\Color)
            EndWith
        Next
        StopDrawing()
        SaveImage(img,file$,#PB_ImagePlugin_PNG)
    EndIf
EndProcedure
Procedure OpenMainForm()
    Protected flag=#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_Maximize
    OpenWindow(#MainForm,0,0,800,600,"Icone maker",flag)
    ButtonGadget(#BtImport,10,10,90,30,"Import")
    ButtonGadget(#BtSave,200,10,90,30,"Save")
    SpinGadget(#SpinSize,100,10,90,30,12,128,#PB_Spin_Numeric)
    CanvasGadget(#MainCanvas,0,40,WindowWidth(#MainForm),WindowHeight(#MainForm)-40)
    BindEvent(#PB_Event_CloseWindow,@CloseMainForm(),#MainForm)
    BindGadgetEvent(#BtImport,@EventBtImport())
    BindGadgetEvent(#SpinSize,@EventSpinSize())
    BindGadgetEvent(#BtSave,@EventSave())
    SetGadgetState(#SpinSize,64)
    SetGadgetText(#SpinSize,"64")
    Draw()
EndProcedure
OpenMainForm()
Repeat:WaitWindowEvent():ForEver
Windows 10 64 bits PB: 5.70 ; 5.72 LST
Work at Centre Spatial de Liège