WindowTitle {Color Selector}
WindowSize 316 280
Font	-adobe-helvetica-bold-r-*-*-12-*

Init
Begin
    Set $Lang = {}
    Set $Color = {}
    Set $Launched = 0
    Set $return = {}
    # Arguments
    Set $i = 1
    Set $arg = (GetScriptArgument $i)
    While $arg <> {} Do
    Begin
	Set $j = (Add $i 1)
	If $arg == {--viewer-colorset} Then
	    Set $ViewerColorset = (GetScriptArgument $j)
	If $arg == {--launched} Then
	    Set $Launched = (GetScriptArgument $j)
 	If $arg == {--lang} Then
	    Set $Lang = (GetScriptArgument $j)
	If $arg == {--color} Then
	    Set $Color = (GetScriptArgument $j)
	Set $i = (Add $i 2)
	Set $arg = (GetScriptArgument $i)
    End

    If $ViewerColorset <> {} Then
    Begin
	ChangeColorset 998 $ViewerColorset
    End

    Set $SelFont = {-adobe-helvetica-bold-r-*-*-12-*}
    Set $ShadeFont = {-adobe-helvetica-medium-r-*-*-12-*}
    Set $SaveFont = {Sel}
    Set $nonDefaultFont = 0

    #Set $Lang = {en}
    #Set $Lang = {ru}
    # ------------------------------------- Internationalization.
    Set $DocLoaded = 0
    Set $nonDefaultFont = 0

    If $Lang == {} Then
    Begin
	Set $cmd = {echo "$LANG" | cut -c1-2}
	Set $Lang = (GetOutput $cmd 1 -1)
	Set $localeSet = 1
    End
    If $Lang == {} Then
	Set $Lang = {C}

    Set $msg = {}
    Set $id = {}
    Set $cmd = {fvwm-themes-script --get-msg }FvwmScript-ColorSelector{ --lang }$Lang
    Set $MSG = (GetOutput $cmd 1 -1)
    Set $index = 0
    Set $notdone = 1
    While $notdone == 1 Do
    Begin
	Set $index = (Add $index 1)
	Set $id = (StrCopy $MSG $index (Add $index 11))
	Set $index = (Add $index 12)
	Set $tmp = (StrCopy $MSG $index (Add $index 3))
	Set $index = (Add $index 4)
	Set $pos = (Add $index $tmp)
	Set $msg = (StrCopy $MSG $index $pos)
	Set $index = (Add $index $tmp)
	#Do {Echo id:}$id{:msg:}$msg{:}

	If $index > 9999 Then
	    Set $notdone = 0
	If $id == {END         } Then
	    Set $notdone = 0
	If $id == {} Then
	    Set $notdone = 0
	# TS
	If $id == {Title       } Then
	    ChangeTitle 1 $msg
	If $id == {SelColor    } Then
	    ChangeTitle 14 $msg
	If $id == {Help        } Then
	    ChangeTitle 993 $msg
	If $id == {Ok          } Then
	Begin
	    ChangeTitle 992 $msg
	    ChangeTitle 999 $msg
	End
	If $id == {Cancel      } Then
	    ChangeTitle 994 $msg
	If $id == {ChangeFont  } Then
	    Set $nonDefaultFont = $msg
	If $id == {SelFont     } Then
	    Set $SelFont = $msg
	If $id == {ShadeFont   } Then
	    Set $ShadeFont = $msg
	If $id == {FixedFont   } Then
	    Set $FixedFont = $msg
    End

    If $nonDefaultFont == 1 Then
    Begin
	ChangeFont 1 $SelFont
	ChangeFont 14 $SelFont
	ChangeFont 17 $SelFont
	ChangeFont 30 $SelFont
	ChangeFont 32 $SelFont
	ChangeFont 34 $SelFont
	ChangeFont 992 $SelFont
	ChangeFont 993 $SelFont
    	ChangeFont 994 $SelFont
	# when the doc will be written
	# ChangeFont 994 $FixedFont
	ChangeFont 999 $SelFont
    End

    # ---------------------- Color init

    Set $cmd = {echo "$FT_DATADIR"}
    Set $FtDataDir = (GetOutput $cmd 1 -1)
    Set $RainbowFile = $FtDataDir{/images/fvwmscript/pallete-rainbow.xpm}
    Set $GreyFile    = $FtDataDir{/images/fvwmscript/pallete-gray.xpm}

    Set $cmd = {cat }$RainbowFile
    Set $ColorLine = (GetOutput $cmd 282 -1)
    Set $cmd = {cat }$GreyFile
    Set $GreyColorLine = (GetOutput $cmd 282 -1)

    If $Color <> {} Then
	Set $CurrentColor = $Color
    Else
	Set $CurrentColor = {#02EEFF}

    Set $UpdateColors = 1
    Set $IsSecond = 0
    Do {*FvwmTheme: Colorset 29 fg black, bg #908090, HGradient 190 2 #000000 50 }$CurrentColor{ 50 #ffffff, NoShape}
    Set $LightColor = $CurrentColor
    Set $LightNumRed = (HexToNum (StrCopy $LightColor 2 3))
    Set $LightNumGreen = (HexToNum (StrCopy $LightColor 4 5))
    Set $LightNumBlue = (HexToNum (StrCopy $LightColor 6 7))
End

#----------------------------------------------------------------------------

PeriodicTasks
Begin
    
    If $UpdateColors == 1 Then
    Begin
	Set $UpdateColors = 0
	ChangeBackColor 15 $CurrentColor
	ChangeTitle 17 $CurrentColor
	Set $Red = (StrCopy $CurrentColor 2 3)
	Set $Green = (StrCopy $CurrentColor 4 5)
	Set $Blue = (StrCopy $CurrentColor 6 7)
	Set $NumRed = (HexToNum $Red)
	Set $NumGreen = (HexToNum $Green)
	Set $NumBlue = (HexToNum $Blue)
	Set $NumR = $NumRed
	Set $NumG = $NumGreen
	Set $NumB = $NumBlue
	Set $t ={R: } $Red
	ChangeTitle 30 $t
	ChangeValue 31 $NumRed
	Set $t ={G: } $Green
	ChangeTitle 32 $t
	ChangeValue 33 $NumGreen
	Set $t ={B: } $Blue
	ChangeTitle 34 $t
	ChangeValue 35 $NumBlue
    End

End

# ----------------------------------------------------------------------------

# Title
Widget 1
Property
    Flags NoReliefString
    Position 10 5
    Size 296 20
    Type ItemDraw
    Title {}
Main
Case message of
 SingleClic :
 Begin
 End
End

# main rectangle
Widget 2
Property
    Flags NoReliefString
    Position 10 25
    Size 296 115
    Type Rectangle
    Title {}
Main
Case message of
 SingleClic :
 Begin
 End
End

#first rainbow
Widget 10
Property
    Flags NoReliefString
    Position 30 35
    Size 256 20
    Type ItemDraw
    Title {}
    Icon fvwmscript/pallete-rainbow.xpm
Main
Case message of
 SingleClic :
 Begin
    Set $X = (GetMinValue 10)
    Set $index = (Mult $X 2)
    Set $index = (Add $index 2)
    Set $end = (Add $index 1)
    Set $name = (StrCopy $ColorLine $index $end)
    Set $a = (StrCopy $name 1 1)
    Set $b = (StrCopy $name 2 2)
    If $a == {^} Then
	Set $a={\^}
    If $b == {^} Then
	Set $b={\^}
    If $a == {`} Then
	Set $a={\`}
    If $b == {`} Then
	Set $b={\`}
    # do we have to protec other characters?	
    Set $cmd = {grep "[}$a{][}$b{] c #" }$RainbowFile
    Set $tmp = (GetOutput $cmd 1 -1)
    Set $LightColor = (StrCopy $tmp 7 19)
    Set $LightRed = (StrCopy $LightColor 2 3)
    Set $LightGreen = (StrCopy $LightColor 6 7)
    Set $LightBlue = (StrCopy $LightColor 10 11)
    Set $LightColor = {#} $LightRed $LightGreen $LightBlue
    Do {*FvwmTheme: Colorset 29 fg black, bg #908090, HGradient 256 2 #000000 50 }$LightColor{ 50 #ffffff, NoShape}
    Set $LightNumRed = (HexToNum $LightRed)
    Set $LightNumGreen = (HexToNum $LightGreen)
    Set $LightNumBlue = (HexToNum $LightBlue)
    Set $IsSecond = 0
 End
End

#second rainbow
Widget 13
Property
    Flags NoReliefString
    Position 30 60
    Size 256 20
    Type ItemDraw
    Title {}
    Icon fvwmscript/pallete-rainbow.xpm
Main
Case message of
 SingleClic :
 Begin
    Set $X = (GetMinValue 13)
    Set $index = (Mult $X 2)
    Set $index = (Add $index 2)
    Set $end = (Add $index 1)
    Set $name = (StrCopy $ColorLine $index $end)
    Set $a = (StrCopy $name 1 1)
    Set $b = (StrCopy $name 2 2)
    If $a == {^} Then
	Set $a={\^}
    If $b == {^} Then
	Set $b={\^}
    If $a == {`} Then
	Set $a={\`}
    If $b == {`} Then
	Set $b={\`}	
    # do we have to protec other characters?	
    Set $cmd = {grep "[}$a{][}$b{] c #" }$RainbowFile
    Set $tmp = (GetOutput $cmd 1 -1)
    Set $SecondColor = (StrCopy $tmp 7 19)
    Set $SecondRed = (StrCopy $SecondColor 2 3)
    Set $SecondGreen = (StrCopy $SecondColor 6 7)
    Set $SecondBlue = (StrCopy $SecondColor 10 11)
    Set $SecondColor = {#} $SecondRed $SecondGreen $SecondBlue
    Do {*FvwmTheme: Colorset 29 fg black, bg #908090, HGradient 256 }$SecondColor{ }$CurrentColor{, NoShape}
    Set $SecondNumRed = (HexToNum $SecondRed)
    Set $SecondNumGreen = (HexToNum $SecondGreen)
    Set $SecondNumBlue = (HexToNum $SecondBlue)
    Set $OrigNumRed = $NumRed
    Set $OrigNumGreen = $NumGreen
    Set $OrigNumBlue = $NumBlue
    Set $IsSecond = 1
 End
End

# grey
Widget 11
Property
    Flags NoReliefString
    Position 30 85
    Size 256 20
    Type ItemDraw
    Title {}
    Icon fvwmscript/pallete-gray.xpm
Main
Case message of
 SingleClic :
 Begin
    Set $X = (GetMinValue 11)
    Set $index = (Mult $X 2)
    Set $index = (Add $index 2)
    Set $end = (Add $index 1)
    Set $name = (StrCopy $GreyColorLine $index $end)
    Set $a = (StrCopy $name 1 1)
    Set $b = (StrCopy $name 2 2)
    If $a == {^} Then
	Set $a={\^}
    If $b == {^} Then
	Set $b={\^}
    If $a == {`} Then
	Set $a={\`}
    If $b == {`} Then
	Set $b={\`}
    # do we have to protec other characters?	
    Set $cmd = {grep "[}$a{][}$b{] c #" }$GreyFile
    Set $tmp = (GetOutput $cmd 1 -1)
    Set $SecondColor = (StrCopy $tmp 7 19)
    Set $SecondRed = (StrCopy $SecondColor 2 3)
    Set $SecondGreen = (StrCopy $SecondColor 6 7)
    Set $SecondBlue = (StrCopy $SecondColor 10 11)
    Set $SecondColor = {#} $SecondRed $SecondGreen $SecondBlue
    Do {*FvwmTheme: Colorset 29 fg black, bg #908090, HGradient 256 }$SecondColor{ }$CurrentColor{, NoShape}
    Set $IsSecond = 1
    Set $SecondNumRed = (HexToNum $SecondRed)
    Set $SecondNumGreen = (HexToNum $SecondGreen)
    Set $SecondNumBlue = (HexToNum $SecondBlue)
    Set $OrigNumRed = $NumRed
    Set $OrigNumGreen = $NumGreen
    Set $OrigNumBlue = $NumBlue
 End
End

Widget 12
Property
    Flags NoReliefString
    Position 30 110
    Size 256 20
    Colorset 29
    Type ItemDraw
    Title {}
Main
Case message of
 SingleClic :
 Begin
    Set $X = (GetMinValue 12)
    If $IsSecond == 0 Then
    Begin
	If $X > 128 Then
	Begin
	    Set $X = (Add $X -128)
	    Set $cmd = {perl -e '$ln = }$LightNumRed{; $x = }$X{; $r = $ln + ($x/128)*(256-$ln); $r = int($r); $r = 255 if $r > 255; print "$r\n"'}
	    Set $NumRed = (GetOutput $cmd 1 -1)
	    Set $cmd = {perl -e '$ln = }$LightNumGreen{; $x = }$X{; $r = $ln + ($x/128)*(256-$ln); $r = int($r); $r = 255 if $r > 255; print "$r\n"'}
	    Set $NumGreen = (GetOutput $cmd 1 -1)
	    Set $cmd = {perl -e '$ln = }$LightNumBlue{; $x = }$X{; $r = $ln + ($x/128)*(256-$ln); $r = int($r); $r = 255 if $r > 255; print "$r\n"'}
	    Set $NumBlue = (GetOutput $cmd 1 -1)
	End
	Else
	Begin
	    Set $cmd = {perl -e '$ln = }$LightNumRed{; $x = }$X{; $r = ($x/128)*($ln); $r = int($r); print "$r\n"'}
	    Set $NumRed = (GetOutput $cmd 1 -1)
	    Set $cmd = {perl -e '$ln = }$LightNumGreen{; $x = }$X{; $r = ($x/128)*($ln); $r = int($r);print "$r\n"'}
	    Set $NumGreen = (GetOutput $cmd 1 -1)
	    Set $cmd = {perl -e '$ln = }$LightNumBlue{; $x = }$X{; $r = ($x/128)*($ln); $r = int($r);print "$r\n"'}
	    Set $NumBlue = (GetOutput $cmd 1 -1)
	End
    End
    Else
    Begin
	Set $cmd = {perl -e '$ln = }$OrigNumRed{; $g = }$SecondNumRed{; $x = }$X{; $r = (($ln - $g)*$x)/256 + $g; $r = int($r); print "$r\n"'}
	    Set $NumRed = (GetOutput $cmd 1 -1)
	    Set $cmd = {perl -e '$ln = }$OrigNumGreen{; $g = }$SecondNumGreen{; $x = }$X{; $r = (($ln - $g)*$x)/256 + $g; $r = int($r); print "$r\n"'}
	    Set $NumGreen = (GetOutput $cmd 1 -1)
	    Set $cmd ={perl -e '$ln = }$OrigNumBlue{; $g = }$SecondNumBlue{; $x = }$X{; $r = (($ln - $g)*$x)/256 + $g; $r = int($r); print "$r\n"'}
	    Set $NumBlue = (GetOutput $cmd 1 -1)
    End
    Set $Red = (NumToHex $NumRed 2)
    Set $Green = (NumToHex $NumGreen 2)
    Set $Blue = (NumToHex $NumBlue 2)
    Set $CurrentColor = {#} $Red $Green $Blue
    Set $UpdateColors = 1
 End
End

# Selected Color (title):
Widget 14
Property
    Flags NoReliefString
    Position 5 152
    Size 145 20
    Type ItemDraw
    Title {}
Main
Case message of
 SingleClic :
 Begin
 End
End

# The color
Widget 15
Property
    Flags NoReliefString
    Position 158 153
    Size 50 20
    Type ItemDraw
    Title {}
Main
Case message of
 SingleClic :
 Begin
    Set $LightRed = $Red
    Set $LightGreen = $Green
    Set $LightBlue = $Blue
    Set $LightColor = {#} $LightRed $LightGreen $LightBlue
    Do {*FvwmTheme: Colorset 29 fg black, bg #908090, HGradient 256 2 #000000 50 }$LightColor{ 50 #ffffff, NoShape}
    Set $LightNumRed = $NumRed
    Set $LightNumGreen = $NumGreen
    Set $LightNumBlue = $NumBlue
    Set $IsSecond = 0
 End
End

Widget 16
Property
    Flags NoReliefString
    Position 157 152
    Size 52 22
    Type Rectangle
    Title {}
Main
Case message of
 SingleClic :
 Begin
 End
End

# color in hex
Widget 17
Property
    Flags NoReliefString
    Position 220 155
    Size 70 15
    Type ItemDraw
    Title {}
Main
Case message of
 SingleClic :
 Begin
 End
End

# R
Widget 30
Property
    Flags NoReliefString
    Position 20 195
    Size  40 15
    Type ItemDraw
    Title {}
Main
Case message of
 SingleClic :
 Begin
 End
End

Widget 31
Property
 Size 1 1
 Position 70 185
 Type MiniScroll
 Flags NoReliefString
 Font fixed
 MinValue 0
 MaxValue 255
Main
 Case message of
  SingleClic :
  Begin
    Set $NumR = (GetValue 31)
    Set $NumG = (GetValue 33)
    Set $NumB = (GetValue 35)
    Set $test = (Mult $NumR -1)
    Set $test = (Add $test $NumRed)
    If $test > 200 Then
	ChangeValue 31 255
    Set $test = (Mult $NumRed -1)
    Set $test = (Add $test $NumR)
    If $test > 200 Then
	ChangeValue 31 0
    Set $NumRed = (GetValue 31)
    Set $NumGreen = (GetValue 33)
    Set $NumBlue = (GetValue 35)
    Set $Red = (NumToHex $NumRed 2)
    Set $Green = (NumToHex $NumGreen 2)
    Set $Blue = (NumToHex $NumBlue 2)
    Set $CurrentColor = {#} $Red $Green $Blue
    ChangeBackColor 15 $CurrentColor
    ChangeTitle 17 $CurrentColor
    Set $t ={R: } $Red
    ChangeTitle 30 $t
    Set $t ={G: } $Green
    ChangeTitle 32 $t
    Set $t ={B: } $Blue
    ChangeTitle 34 $t
  End
End

# G
Widget 32
Property
    Flags NoReliefString
    Position 110 195
    Size  40 15
    Type ItemDraw
    Title {}
Main
Case message of
 SingleClic :
 Begin
 End
End

Widget 33
Property
 Font fixed   
 Size 1 1
 Position 160 185
 Type MiniScroll
 Flags NoReliefString
 Value 196
 MinValue 1
 MaxValue 255
Main
 Case message of
  SingleClic :
  Begin
    Set $NumR = (GetValue 31)
    Set $NumG = (GetValue 33)
    Set $NumB = (GetValue 35)
    Set $test = (Mult $NumG -1)
    Set $test = (Add $test $NumGreen)
    If $test > 200 Then
	ChangeValue 33 255
    Set $test = (Mult $NumGreen -1)
    Set $test = (Add $test $NumG)
    If $test > 200 Then
	ChangeValue 33 0
    Set $NumRed = (GetValue 31)
    Set $NumGreen = (GetValue 33)
    Set $NumBlue = (GetValue 35)
    Set $Red = (NumToHex $NumRed 2)
    Set $Green = (NumToHex $NumGreen 2)
    Set $Blue = (NumToHex $NumBlue 2)
    Set $CurrentColor = {#} $Red $Green $Blue
    ChangeBackColor 15 $CurrentColor
    ChangeTitle 17 $CurrentColor
    Set $t ={R: } $Red
    ChangeTitle 30 $t
    ChangeValue 31 $NumRed
    Set $t ={G: } $Green
    ChangeTitle 32 $t
    ChangeValue 33 $NumGreen
    Set $t ={B: } $Blue
    ChangeTitle 34 $t
    ChangeValue 35 $NumBlue
  End
End

# B
Widget 34
Property
    Flags NoReliefString
    Position 200 195
    Size  40 15
    Type ItemDraw
    Title {}
Main
Case message of
 SingleClic :
 Begin
 End
End

Widget 35
Property
 Size 250 1
 Position 250 185
 Type MiniScroll
 Flags NoReliefString
 Font fixed
 Value 184
 MinValue 0
 MaxValue 255
Main
 Case message of
  SingleClic :
  Begin
    Set $NumR = (GetValue 31)
    Set $NumG = (GetValue 33)
    Set $NumB = (GetValue 35)
    Set $test = (Mult $NumB -1)
    Set $test = (Add $test $NumBlue)
    If $test > 200 Then
	ChangeValue 35 255
    Set $test = (Mult $NumBlue -1)
    Set $test = (Add $test $NumB)
    If $test > 200 Then
	ChangeValue 35 0
    Set $NumRed = (GetValue 31)
    Set $NumGreen = (GetValue 33)
    Set $NumBlue = (GetValue 35)
    Set $Red = (NumToHex $NumRed 2)
    Set $Green = (NumToHex $NumGreen 2)
    Set $Blue = (NumToHex $NumBlue 2)
    Set $CurrentColor = {#} $Red $Green $Blue
    ChangeBackColor 15 $CurrentColor
    ChangeTitle 17 $CurrentColor
    Set $t ={R: } $Red
    ChangeTitle 30 $t
    ChangeValue 31 $NumRed
    Set $t ={G: } $Green
    ChangeTitle 32 $t
    ChangeValue 33 $NumGreen
    Set $t ={B: } $Blue
    ChangeTitle 34 $t
    ChangeValue 35 $NumBlue
  End
End

#----------------------------------------------------------------------------

# Ok
Widget 992
Property
    Flags NoReliefString
    Position 20 245
    Size 80 20
    #Font fixed
    Type PushButton
    Title {OK}
Main
Case message of
 SingleClic :
 Begin
    If $Launched <> {} Then
    Begin
	Set $ret = $CurrentColor
	SendToScript (GetScriptFather) $ret
    End
    Quit
 End
End

# Help
Widget 993
Property
    Flags NoReliefString
    Position 120 245
    Size 80 20
    #Font fixed
    Type PushButton
    Title {}
Main
Case message of
 SingleClic :
 Begin
    HideWidget 1
    #HideWidget 20
    Set $tmp = 10
    While $tmp <> 18 Do
    Begin
	HideWidget $tmp
	Set $tmp = (Add $tmp 1)
    End
    Set $tmp = 30
    While $tmp <> 36 Do
    Begin
	HideWidget $tmp
	Set $tmp = (Add $tmp 1)
    End
    HideWidget 994
    HideWidget 993
    HideWidget 992
    If $DocLoaded == 0 Then
    Begin
	Set $DocLoaded = 1
	Set $cmd = {fvwm-themes-script --in-line-doc FvwmScript-ColorSelector --line-length 44 --lang }$Lang
	Set $Doc = (GetOutput $cmd 1 -1)

        # first "line" is the font, evaluate it
        Set $font = {}
        Set $index = 1
        Set $done = 0
        While $done <> 1 Do
        Begin
            Set $tmp = (StrCopy $Doc $index $index)
            If $tmp == {|} Then
                Set $done = 1
            Else
                Set $font = $font $tmp 
            Set $index = (Add $index 1)
            If $index == 300 Then
            Begin
                Set $font = "fixed"
                Set $done = 1
            End
        End
	ChangeFont 998 $font

	Set $Doc = (StrCopy $Doc $index 32000)
	ChangeTitle 998 $Doc
    End
    ShowWidget 998
    ShowWidget 999
 End
End

# Cancel
Widget 994
Property
    Flags NoReliefString
    Position 220 245
    Size 80 20
    #Font fixed
    Type PushButton
    Title {}
Main
Case message of
 SingleClic :
 Begin
    If $Launched <> {} Then
	SendToScript (GetScriptFather) {Cancel}
    Quit
 End
End


Widget 998
Property
    Position 5 5
    Size 306 230
    Type List
    Font fixed
    Flags NoReliefString Hidden
    Title {}

Main
Case message of
 SingleClic :
 Begin
 End
End


# Ok for the doc
Widget 999
Property
    Position 220 245
    Size 80 20
    #Font fixed
    Type PushButton
    Flags NoReliefString Hidden
    Title {}
Main
Case message of
 SingleClic :
 Begin
    ShowWidget 1
    ShowWidget 2
    #ShowWidget 20
    Set $tmp = 10
    While $tmp <> 18 Do
    Begin
	ShowWidget $tmp
	Set $tmp = (Add $tmp 1)
    End
    Set $tmp = 30
    While $tmp <> 36 Do
    Begin
	ShowWidget $tmp
	Set $tmp = (Add $tmp 1)
    End
    ShowWidget 994
    ShowWidget 993
    ShowWidget 992
    HideWidget 998
    HideWidget 999
 End
End
