#
# This file is a part of the NsCDE - Not so Common Desktop Environment
# Author: Hegel3DReloaded
# Licence: GPLv3
#

UseGettext {$NSCDE_ROOT/share/locale;NsCDE-ColorMgr}
WindowLocaleTitle {Style Manager - Modify Color}
WindowSize 464 562
Colorset 22

Init
Begin

   # Set different font for some elements
   Set $DefaultFont = (GetOutput {$NSCDE_TOOLSDIR/getfont -v -t normal -s medium -Z 12} 1 -1)
   Set $SmallFont = (GetOutput {$NSCDE_TOOLSDIR/getfont -v -t normal -s small -Z 10} 1 -1)
   ChangeFont 1 $DefaultFont
   ChangeFont 5 $DefaultFont
   ChangeFont 6 $DefaultFont
   ChangeFont 8 $DefaultFont
   ChangeFont 9 $SmallFont
   ChangeFont 10 $DefaultFont
   ChangeFont 11 $SmallFont
   ChangeFont 12 $DefaultFont
   ChangeFont 13 $SmallFont
   ChangeFont 14 $DefaultFont
   ChangeFont 15 $SmallFont
   ChangeFont 16 $DefaultFont
   ChangeFont 17 $SmallFont
   ChangeFont 18 $DefaultFont
   ChangeFont 19 $SmallFont

   ChangeFont 21 $DefaultFont
   ChangeFont 22 $DefaultFont
   ChangeFont 23 $DefaultFont

   # Read which palette part we are modifying: from 1 to 8
   # Colorsets: 61 - 68
   Set $ColorsetFromPalette = (GetScriptArgument 1)
   If $ColorsetFromPalette == {} Then
   Begin
      Set $ColorsetFromPalette = 1
   End

   # Take background color of the choosen part of the palette
   # intended for the modification
   Set $HexColorArg = (GetScriptArgument 2)
   If $HexColorArg == {} Then
   Begin
      Set $HexColorArg = {#888888}
   End

   # Change Colorset of the "Old" color (Widget 3)
   ChangeColorset 3 $ColorsetFromPalette

   # Set Colorset 69 for "New" color (Widget 4)
   Do {PipeRead "$[NSCDE_TOOLSDIR]/palette_colorgen -T '#} $HexColorArg {'"}

   # Parse hex color from the arg 1, set widget values
   Set $R = (HexToNum (StrCopy $HexColorArg 1 2))
   Set $G = (HexToNum (StrCopy $HexColorArg 3 4))
   Set $B = (HexToNum (StrCopy $HexColorArg 5 6))
   ChangeValue 9 $R
   ChangeValue 11 $G
   ChangeValue 13 $B
   Set $HSVCmd = {$NSCDE_TOOLSDIR/colorconv -R } $R {/} $G {/} $B
   Set $HSV = (GetOutput $HSVCmd 1 -1)
   Set $H = (StrCopy $HSV 1 3)
   Set $S = (StrCopy $HSV 5 7)
   Set $V = (StrCopy $HSV 9 11)
   ChangeValue 19 $H
   ChangeValue 15 $S
   ChangeValue 17 $V

   # Keybindings
   Key Escape A 22 1 {KeyClose}
   Key Return C 21 1 {KeyOk}
   Key G A 5 1 {KeyGrab}
   Key F1 A 23 1 {KeyHelp}
   Key Help A 23 1 {KeyHelp}
End

Widget 1
   Property
   Size 4 0
   Position 22 4
   Type ItemDraw
   Flags NoReliefString Left NoFocus
   LocaleTitle {Color Sample}
   Font "xft:::pixelsize=15"
   Main
      Case message of
      SingleClic :
      Begin
      End
End

Widget 2
   Property
   Size 446 100
   Position 8 12
   Type Rectangle
End

Widget 3
   Property
   Size 72 72
   Position 22 28
   Type PushButton
   LocaleTitle {Old}
   Flags NoReliefString NoFocus
   Colorset 2
   Main
      Case message of
      SingleClic :
      Begin
      End
End

Widget 4
   Property
   Size 72 72
   Position 106 28
   Type PushButton
   LocaleTitle {New}
   Flags NoReliefString NoFocus
   Colorset 69
   Main
      Case message of
      SingleClic :
      Begin
      End
End

Widget 5
   Property
   Size 102 22
   Position 340 28
   Type PushButton
   LocaleTitle {Grab Color}
   Flags NoReliefString
   Font "xft:::pixelsize=15"
   Colorset 22
   Main
      Case message of
      SingleClic :
      Begin
         SendSignal 5 1
      End
      1 :
      Begin
         Set $GrabCmd = {$NSCDE_TOOLSDIR/colorpicker -od}
         Set $GrabHEX = (GetOutput $GrabCmd 1 -1)
         Set $CheckGrabHex = (StrCopy $GrabHEX 1 1)
         If $CheckGrabHex == {#} Then
         Begin
            Do {PipeRead "$[NSCDE_TOOLSDIR]/palette_colorgen -T '#} $GrabHEX {'"}
            Set $R = (HexToNum (StrCopy $GrabHEX 2 3))
            Set $G = (HexToNum (StrCopy $GrabHEX 4 5))
            Set $B = (HexToNum (StrCopy $GrabHEX 6 7))
            ChangeValue 9 $R
            ChangeValue 11 $G
            ChangeValue 13 $B
            Set $HSVCmd = {$NSCDE_TOOLSDIR/colorconv -R } $R {/} $G {/} $B
            Set $HSV = (GetOutput $HSVCmd 1 -1)
            Set $H = (StrCopy $HSV 1 3)
            Set $S = (StrCopy $HSV 5 7)
            Set $V = (StrCopy $HSV 9 11)
            ChangeValue 19 $H
            ChangeValue 15 $S
            ChangeValue 17 $V
            Set $BuildHexForTest = {#} (NumToHex $R 2) (NumToHex $G 2) (NumToHex $B 2)
         End
         Else
         Begin
            Do {f_Notifier "Color Style Manager" "Dismiss" "NsCDE/Error.xpm" "$[gt.Error occured while calling Color Selector:] \\\"} $GrabHEX {\\\"}
         End
      End
End

Widget 6
   Property
   Size 4 0
   Position 22 114
   Type ItemDraw
   Flags NoReliefString Left NoFocus
   LocaleTitle {Color Editor}
   Font "xft:::pixelsize=15"
   Main
      Case message of
      SingleClic :
      Begin
      End
End

Widget 7
   Property
   Size 446 378
   Position 8 122
   Type Rectangle
End

Widget 8
   Property
   Size 4 0
   Position 28 176
   Type ItemDraw
   Flags NoReliefString Left NoFocus
   LocaleTitle {R}
   Font "xft:::pixelsize=15"
   Main
      Case message of
      SingleClic :
      Begin
      End
End

Widget 9
   Property
   Size 310 0
   Position 52 154
   Type HScrollBar
   Flags NoReliefString Left
   MinValue 0
   MaxValue 255
   Title {}
   Font "xft:::pixelsize=10"
   Main
      Case message of
      SingleClic :
      Begin
         Set $R_forHSVCmd = {$NSCDE_TOOLSDIR/colorconv -R '} (GetValue 9) {/} (GetValue 11) {/} (GetValue 13) {'}
         Set $R_forHSV = (GetOutput $R_forHSVCmd 1 -1)
         Set $H = (StrCopy $R_forHSV 1 3)
         Set $S = (StrCopy $R_forHSV 5 7)
         Set $V = (StrCopy $R_forHSV 9 11)
         ChangeValue 19 $H
         ChangeValue 15 $S
         ChangeValue 17 $V
         Set $BuildHexForTest = {#} (NumToHex (GetValue 9) 2) (NumToHex (GetValue 11) 2) (NumToHex (GetValue 13) 2)
         Do {PipeRead "$[NSCDE_TOOLSDIR]/palette_colorgen -T '#} $BuildHexForTest {'"}
      End
End

Widget 10
   Property
   Size 4 0
   Position 28 242
   Type ItemDraw
   Flags NoReliefString Left NoFocus
   LocaleTitle {G}
   Font "xft:::pixelsize=15"
   Main
      Case message of
      SingleClic :
      Begin
      End
End

Widget 11
   Property
   Size 310 0
   Position 52 220
   Type HScrollBar
   Flags NoReliefString Left
   MinValue 0
   MaxValue 255
   Title {}
   Font "xft:::pixelsize=10"
   Main
      Case message of
      SingleClic :
      Begin
         Set $G_forHSVCmd = {$NSCDE_TOOLSDIR/colorconv -R '} (GetValue 9) {/} (GetValue 11) {/} (GetValue 13) {'}
         Set $G_forHSV = (GetOutput $G_forHSVCmd 1 -1)
         Set $H = (StrCopy $G_forHSV 1 3)
         Set $S = (StrCopy $G_forHSV 5 7)
         Set $V = (StrCopy $G_forHSV 9 11)
         ChangeValue 19 $H
         ChangeValue 15 $S
         ChangeValue 17 $V
         Set $BuildHexForTest = {#} (NumToHex (GetValue 9) 2) (NumToHex (GetValue 11) 2) (NumToHex (GetValue 13) 2)
         Do {PipeRead "$[NSCDE_TOOLSDIR]/palette_colorgen -T '#} $BuildHexForTest {'"}
      End
End

Widget 12
   Property
   Size 4 0
   Position 28 304
   Type ItemDraw
   Flags NoReliefString Left NoFocus
   LocaleTitle {B}
   Font "xft:::pixelsize=15"
   Main
      Case message of
      SingleClic :
      Begin
      End
End

Widget 13
   Property
   Size 310 0
   Position 52 284
   Type HScrollBar
   Flags NoReliefString Left
   MinValue 0
   MaxValue 255
   Title {}
   Font "xft:::pixelsize=10"
   Main
      Case message of
      SingleClic :
      Begin
         Set $B_forHSVCmd = {$NSCDE_TOOLSDIR/colorconv -R '} (GetValue 9) {/} (GetValue 11) {/} (GetValue 13) {'}
         Set $B_forHSV = (GetOutput $B_forHSVCmd 1 -1)
         Set $H = (StrCopy $B_forHSV 1 3)
         Set $S = (StrCopy $B_forHSV 5 7)
         Set $V = (StrCopy $B_forHSV 9 11)
         ChangeValue 19 $H
         ChangeValue 15 $S
         ChangeValue 17 $V
         Set $BuildHexForTest = {#} (NumToHex (GetValue 9) 2) (NumToHex (GetValue 11) 2) (NumToHex (GetValue 13) 2)
         Do {PipeRead "$[NSCDE_TOOLSDIR]/palette_colorgen -T '#} $BuildHexForTest {'"}
      End
End

Widget 14
   Property
   Size 4 0
   Position 20 390
   Type ItemDraw
   Flags NoReliefString Left NoFocus
   Title {}
   Font "xft:::pixelsize=15"
   Icon NsCDE/Saturation.xpm
   Main
      Case message of
      SingleClic :
      Begin
      End
End

Widget 15
   Property
   Size 310 0
   Position 52 370
   Type HScrollBar
   Flags NoReliefString Left
   MinValue 0
   MaxValue 255
   Title {}
   Font "xft:::pixelsize=10"
   Main
      Case message of
      SingleClic :
      Begin
         Set $S_forRGBCmd = {$NSCDE_TOOLSDIR/colorconv -H '} (GetValue 19) {/} (GetValue 15) {/} (GetValue 17) {'}
         Set $S_forRGB = (GetOutput $S_forRGBCmd 1 -1)
         Set $R = (StrCopy $S_forRGB 1 3)
         Set $G = (StrCopy $S_forRGB 5 7)
         Set $B = (StrCopy $S_forRGB 9 11)
         ChangeValue 9 $R
         ChangeValue 11 $G
         ChangeValue 13 $B
         Set $BuildHexForTest = {#} (NumToHex $R 2) (NumToHex $G 2) (NumToHex $B 2)
         Do {PipeRead "$[NSCDE_TOOLSDIR]/palette_colorgen -T '#} $BuildHexForTest {'"}
      End
End

Widget 16
   Property
   Size 4 0
   Position 24 454
   Type ItemDraw
   Flags NoReliefString Left NoFocus
   Title {}
   Font "xft:::pixelsize=15"
   Icon NsCDE/Value.xpm
   Main
      Case message of
      SingleClic :
      Begin
      End
End

Widget 17
   Property
   Size 310 0
   Position 52 434
   Type HScrollBar
   Flags NoReliefString Left
   MinValue 0
   MaxValue 255
   Title {}
   Font "xft:::pixelsize=10"
   Main
      Case message of
      SingleClic :
      Begin
         Set $V_forRGBCmd = {$NSCDE_TOOLSDIR/colorconv -H '} (GetValue 19) {/} (GetValue 15) {/} (GetValue 17) {'}
         Set $V_forRGB = (GetOutput $V_forRGBCmd 1 -1)
         Set $R = (StrCopy $V_forRGB 1 3)
         Set $G = (StrCopy $V_forRGB 5 7)
         Set $B = (StrCopy $V_forRGB 9 11)
         ChangeValue 9 $R
         ChangeValue 11 $G
         ChangeValue 13 $B
         Set $BuildHexForTest = {#} (NumToHex $R 2) (NumToHex $G 2) (NumToHex $B 2)
         Do {PipeRead "$[NSCDE_TOOLSDIR]/palette_colorgen -T '#} $BuildHexForTest {'"}
      End
End

Widget 18
   Property
   Size 4 0
   Position 388 132
   Type ItemDraw
   Flags NoReliefString Center NoFocus
   LocaleTitle {Hue}
   Font "xft:::pixelsize=15"
   Main
      Case message of
      SingleClic :
      Begin
      End
End

Widget 19
   Property
   Size 0 180
   Position 366 160
   Type VScrollBar
   Flags NoReliefString Left
   MinValue 0
   MaxValue 359
   Title {}
   Font "xft:::pixelsize=12"
   Main
      Case message of
      SingleClic :
      Begin
         Set $H_forRGBCmd = {$NSCDE_TOOLSDIR/colorconv -H '} (GetValue 19) {/} (GetValue 15) {/} (GetValue 17) {'}
         Set $H_forRGB = (GetOutput $H_forRGBCmd 1 -1)
         Set $R = (StrCopy $H_forRGB 1 3)
         Set $G = (StrCopy $H_forRGB 5 7)
         Set $B = (StrCopy $H_forRGB 9 11)
         ChangeValue 9 $R
         ChangeValue 11 $G
         ChangeValue 13 $B
         Set $BuildHexForTest = {#} (NumToHex $R 2) (NumToHex $G 2) (NumToHex $B 2)
         Do {PipeRead "$[NSCDE_TOOLSDIR]/palette_colorgen -T '#} $BuildHexForTest {'"}
      End
End

Widget 20
   Property
   Size 458 0
   Position 2 512
   Type Rectangle
End

Widget 21
   Property
   Size 132 20
   Position 16 524
   Type PushButton
   LocaleTitle {OK}
   Font "xft:::pixelsize=15"
   Flags NoReliefString
   Colorset 22
   Main
      Case message of
      SingleClic :
      Begin
         SendSignal 21 1
      End
      1 :
      Begin
         If $BuildHexForTest <> {} Then
         Begin
            Do {PipeRead "$[NSCDE_TOOLSDIR]/palette_colorgen -S } $ColorsetFromPalette { -T '#} $BuildHexForTest {'"}
            Do {SendToModule ColorMgr SendString 19 1 } $ColorsetFromPalette
         End
         Quit
      End
End

Widget 22
   Property
   Size 132 20
   Position 166 524
   Type PushButton
   LocaleTitle {Cancel}
   Font "xft:::pixelsize=15"
   Flags NoReliefString
   Colorset 22
   Main
      Case message of
      SingleClic :
      Begin
         SendSignal 22 1
      End
      1 :
      Begin
         Quit
      End
End

Widget 23
   Property
   Size 132 20
   Position 316 524
   Type PushButton
   LocaleTitle {Help}
   Font "xft:::pixelsize=15"
   Flags NoReliefString
   Colorset 22
   Main
      Case message of
      SingleClic :
      Begin
         SendSignal 23 1
      End
      1 :
      Begin
         Do {f_DisplayURL "$[gt.Color Style Manager]" html/NsCDE-ColorMgr.html}
      End
End

