What is up with mojo2 SetColor(r,g,b)?

Monkey Forums/Monkey Beginners/What is up with mojo2 SetColor(r,g,b)?

Hero(Posted 2015) [#1]
Strict
Import mojo2

Function Main:Int()
	New Game()
	Return 0
End

Class Game Extends App

	Private
		Field GameCanvas:Canvas
	
	
	Public
	'summary:The OnCreate Method is called when mojo has been initialized and the application has been successfully created.
	Method OnCreate:Int()	
		'Set how many times per second the game should update and render itself
		SetUpdateRate(60)
				
		GameCanvas = New Canvas()
		Return 0
	End
	
	'summary: This method is automatically called when the application's update timer ticks. 
	Method OnUpdate:Int()

		Return 0
	End
	
	'summary: This method is automatically called when the application should render itself, such as when the application first starts, or following an OnUpdate call. 
	Method OnRender:Int()

		GameCanvas.Clear(255, 255, 255)
		GameCanvas.SetColor(0, 0, 255)
		GameCanvas.DrawCircle(DeviceWidth() / 2, DeviceHeight() / 2, 100)
		GameCanvas.Flush()
		Return 0
	End
End


I was expecting to see a blue circle on a white background, but the circle is black. If I want to see a blue circle I need to use

GameCanvas.SetColor(0,255,255)


But why?

The "Hello World" banana/sample from the mojo2 folder has the same (for me) unexpected behaviour. It sais:

canvas.SetColor(255, 0, 0)
canvas.DrawRect( (DeviceWidth / 2) - x / 2, (DeviceHeight / 2) - x / 2, x, x);


Running it shows a green pulsating rect, whereas I am expecting to see a red one.

The documentation describes the Canvas.SetColor method as follows:

SetColor : Void ( red:Float, green:Float, blue:Float )



Playniax(Posted 2015) [#2]
Mojo 2 color values range between 0.0 and 1.0, not 0 and 255.


Hero(Posted 2015) [#3]
Why does the mojo2 hello world example use a value of 255? Also should there not be an error thrown if the value is out of the expected range?


Nobuyuki(Posted 2015) [#4]
the example probably has a mistake in it. Mojo2 is relatively new. No exception is thrown because the behavior depends on the target implementation. In this case, the value is passed directly to OGL, which is passed to your graphics card, and it's very likely the behavior for values outside of this range are undefined. Clamping these values (or doing error checks on them) would be slower than not doing it, so you just end up with whatever you end up with. This -- undefined -- is also (AFAIK) expected behavior on DX as well as OGL targets. So it's up to you to make sure the value does not exceed 0.0-1.0.


Hero(Posted 2015) [#5]
OK. Thanks.

Tho I must say this is propably the first programming language/environment with an error in the hello world example code. Also the documentation is inconsistent aswell

Function SetColor : Int ( r:Float, g:Float, b:Float )


Sets the current color. 

The current color is used by all drawing operations except Cls. 

Note: Drawing images in any color other than 255,255,255 on the HTML5 target will incur a major runtime overhead. For best results on the HTML5 target, either use colored images sparingly, or consider using 'pre-colored' images stored in multiple image files.


The method expects values from 0 to 1 but 3 lines below the note references a color using 0-255 values.

Just saying, I think these points should be fixed because as a programmer comming new to this language it can throw you off..


MikeHart(Posted 2015) [#6]
What you see is the documentation for mojo.SetColor not mojo2.SetColor. Ted is not able to seperate between these and wil always show the first one. The docs for mojo2.SetColor are just fine.