Implemented Colors

Dubbsta

Active Member
Joined
Jul 13, 2017
if i may give a small contribution to cx in the form of colors?
i tried to keep it basic with the most popular colors i could think of. so far 31 colors. its a basic implementation and may need more than i can do.
if it doesnt make the cut no prob.


Cerberus X:
Import mojo

Class Color
 Field color:String
 
        Method set:Void(color:String)
            Self.color = color
            Select color
                Case "forestgreen"
                    SetColor(34,139,34)
                Case "darkgreen"
                    SetColor(0,100,0)
                Case "lightgreen"
                    SetColor(144,238,144)
                Case "lightblue"
                    SetColor(173,216,230)
                Case "blue"
                    SetColor(0,0,255)
                Case "red"
                    SetColor(255,0,0)
                Case "limegreen"
                    SetColor(0,255,0)
                Case "yellow"
                    SetColor(255,255,0)
                Case "orange"
                    SetColor(255,165,0)
                Case "cyan"
                    SetColor(0,255,255)
                Case "gold"
                    SetColor(255,215,0)
                Case "pink"
                    SetColor(255,192,203)
                Case "purple"
                    SetColor(128,0,128)
                Case "violet"
                    SetColor(238,130,238)
                Case "magenta"
                    SetColor(255,0,255)
                Case "navyblue"
                    SetColor(0,0,128)   
                Case "royalblue"
                    SetColor(65,105,255)
                Case "tan"
                    SetColor(210,180,140)
                Case "brown"
                    SetColor(139,69,19)       
                Case "green"
                    SetColor(0,128,0)
                Case "black"
                    SetColor(0,0,0)
                Case "white"
                    SetColor(255,255,255)
                Case "lightgray"
                    SetColor(211,211,211)
                Case "gray"
                    SetColor(128,128,128)
                Case "darkgray"
                    SetColor(169,169,169)
                Case "pastelred"
                    SetColor(254,163,170)
                Case "pastelorange"
                    SetColor(248,184,139)
                Case "pastelyellow"
                    SetColor(250,248,132)
                Case "pastelgreen"
                    SetColor(186,237,145)
                Case "pastelblue"
                    SetColor(178,206,254)
                Case "pastelpurple"
                    SetColor(242,162,232)             
            End
        End
        
        Method get:String()
            Return color
        End
End
[/CODE]
 

Paul59

Active Member
Joined
Dec 13, 2018
Location
UK
Wouldn't these definitions be more useful (perhaps for html5 apps anyway)?

https://www.w3schools.com/colors/colors_names.asp

Could you make it agnostic by passing a canvas (default null) to set()? You could store the color values outside set() in a single look up table and use division to get suitable values for mojo2. Just thoughts ;)
 

Dubbsta

Active Member
Joined
Jul 13, 2017
I'll will wait to see what you got regarding mojo2.
Sorry mike i meant havnt messed with mojo2 at all yet but if uyou dtill want to wait ok

Paul59 im not on mojo2 yet but i will try to make the switch soon. It was just meant for a quick way to set basic colors without look up. As i get better if noone comes up with a better way maybe i can update it.
 
Last edited:

MikeHart

Administrator
Staff member
Joined
Jun 19, 2017
Location
Germany
@Dubbsta is it about a having the color as a seperate object, or the ability to set the color via a string. If later, I woudl prefer a 2nd SetColor version that takes in a String.
 

Holzchopf

Moderator
Staff member
Joined
Jul 31, 2017
Location
Bern, Switzerland
Yes it's a colour class that mainly acts as a colour-space-translator. You can set colours in RGB, HSL, HSB, hex and mix them. As soon as you read or write one of the components, the colour's automatically updated. So you could set the colour in HSB and read it in RGB.
 

Holzchopf

Moderator
Staff member
Joined
Jul 31, 2017
Location
Bern, Switzerland
Workaround-y ;-) Since I didn't consider suggesting the whole class as official, I made custom functions ClearCanvas and ColorCanvas which take a mojo2 canvas and a Color parameter. Of course, having SetColor and Clear methods in Canvas, that accept a "Color", would make at least 1000% more sense
 

Paul59

Active Member
Joined
Dec 13, 2018
Location
UK
Would having 'named' colors really be that useful? How many people would really want to use "red" = (1, 0, 0) in their games - more likely some shade of red I'd think.

It would be great if SetColor() could take a struct (I supposed that's just a class with only fields in CX) so one could write SetColor(c) where c might be defined as a color 'type' with fields r, g, b, a. There again this would be easy enough to do by the user per program with a Map<String, Int> and some shifting, where "red" = $FF0000 for example.
 

Dubbsta

Active Member
Joined
Jul 13, 2017
@Dubbsta is it about a having the color as a separate object, or the ability to set the color via a string. If later, I woudl prefer a 2nd SetColor version that takes in a String.
mike not sure if the question is still relevant but i guess if it sounds like a good idea could be useful for prototyping or games that use solid colors. not sure about the question but it does take a string so it would be....color:Color | color.set("orange").
 

dawlane

Active Member
CX Code Contributor
Joined
Jun 21, 2017
I would haven't used strings as a method to represent a colour constant. Much more memory saving and performance wise to use constant integer codes to represent fixed colours.
There nothing to stop you from setting these constants as full blown RGBA in hex notation and using a bit-wise shifting and masking for each colour element in the SetColor statment.
 

dawlane

Active Member
CX Code Contributor
Joined
Jun 21, 2017
I will be nice and give you a little demo.
Cerberus X:
Strict

Import mojo

Const COLOR_BLUE:Int = $0000ff
Const COLOR_FOREST_GREEN:Int = $228b22
Const COLOR_DK_GREEN:Int = $006400
Const COLOR_LT_GREEN:Int = $90ee90
Const COLOR_LT_BLUE:Int = $aDd8e6
Const COLOR_RED:Int = $ff0000
Const COLOR_LIME_GREEN:Int = $00ff00
Const COLOR_YELLOW:Int = $ffff00
Const COLOR_ORANGE:Int = $ffa500
Const COLOR_CYAN:Int = $00ffff
Const COLOR_GOLD:Int = $ffd700
Const COLOR_PINK:Int = $ffc0cb
Const COLOR_VIOLET:Int = $800080


Class CGame Extends App
    Field colorStack:= New IntStack()
    Field lt:Int
    Field i:Int
   
    Method OnCreate:Int()
        SetUpdateRate(60)
       
        colorStack.Push(COLOR_BLUE)
        colorStack.Push(COLOR_FOREST_GREEN)
        colorStack.Push(COLOR_DK_GREEN)
        colorStack.Push(COLOR_LT_GREEN)
        colorStack.Push(COLOR_LT_BLUE)
        colorStack.Push(COLOR_RED)
        colorStack.Push(COLOR_LIME_GREEN)
        colorStack.Push(COLOR_YELLOW)
        colorStack.Push(COLOR_ORANGE)
        colorStack.Push(COLOR_CYAN)
        colorStack.Push(COLOR_GOLD)
        colorStack.Push(COLOR_PINK)
        colorStack.Push(COLOR_VIOLET)
       
        Return 0
    End Method
   
    Method OnUpdate:Int()
        Local t:Int=Millisecs()
        If t >= lt+2000
            lt=t
            i+=1
            If i>colorStack.Length()-1 i=0
        Endif
        Return 0
    End Method
   
    Method OnRender:Int()
        Cls
        SetColors(colorStack.Get(i))
        DrawText("HI", 0,0)
        DrawRect(220,150,200,200)
        Return 0
    End Method
   
End Class

Function SetColors:Void(c:Int)
    SetColor(c Shr 16 & $ff, c Shr 8 & $ff, c & $ff)
End Function

Function Main:Int()
    New CGame()
    Return 0
End Function
 
Top Bottom