way to convert -2147483846 to useable color values?

Dubbsta

Active member
Joined
Jul 13, 2017
Messages
208
i bit shifted some pixels for the image.WritePixels function and printed them out and get this long number, is there a way to convert this to argb or rgb or something useable? thx
 
Last edited:

MikeHart

Administrator
CX Code Contributor
3rd Party Module Dev
3rd Party Target Dev
3rd Party Tool Dev
Joined
Jun 19, 2017
Messages
3,504
Negative number, no. A positive number can be used as an input for SetColor. Or use the Mojo Color class.
 

Wingnut

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,284
It depends what you are doing but I know that there's a very common misconception here. But I'm not saying it's free from problems either.

It gets notoriously difficult when you use higher bits that has several interpretations at once in different commands, and it's not always obvious how they are handled. Even the IDE and compiler are parts of this. The very second you write a number and it gets converted into binary representation, which often is a signed integer in languages.

I've added a comment inside the function that you can uncomment to validate that they are indeed negative and still handled correctly.
You just have to be really careful. I've checked for precision and I can say that Cerberus-X keeps all information intact well in cases like this.
I've had some problems using more than 12bits in some code I made and that has to be said. In Mojo1 you just shift, here I use Mojo2 where you have to shift and then normalise into 0-1.0. You can do this and back without having any worries about precision getting lost as long as you use no more than 8 bit per component.

If you use more, I don't know what will happen.

See this example I've made just for this :
Code:
Strict
Import mojo2  
Import brl.databuffer

Function Main:Int()
    New Game  
    Return 0
End

Class Game Extends App

    Field checkred:Int = 206, checkgreen:Int = 246, checkblue:Int = 4, checkalpha:Int = 255 ' << Good test
    Field xxx:Int, yyy:Int,screen2:Image, r:Int, g:Int, b:Int, a:Int, sprite:Canvas, canvas:Canvas
    Field cc:Float[4], pixels:DataBuffer = New DataBuffer(4*320*200), ptr:Int = 0

    Method OnCreate:Int()  
        canvas=New Canvas
        SetSwapInterval 1 ; SetUpdateRate 0
        screen2=New Image(320,200,0,0,0) ; sprite=New Canvas(screen2)
        cc[0] = checkred/255.0 ; cc[1] = checkgreen/255.0 ; cc[2] = checkblue/255.0 ; cc[3] = checkalpha/255.0
        sprite.SetColor cc[0],cc[1],cc[2],cc[3]
        sprite.DrawRect 0,0,320,200 ; sprite.Flush    
        Return 0
    End

    Method OnRender:Int()  
        canvas.Clear 0,0,0,0
        canvas.SetColor 1,1,1,1
         For Local temp:Int = 0 To 100 ' x100 faster test
             yyy= Int(ptr/320) ; xxx= ptr-(yyy*320) ; ptr = ptr + 1 ; If ptr > (320*200) Then ptr = 0            
            sprite.ReadPixels(xxx,yyy,1,1, pixels)
            Local pixel:Int[] = ColorToRgb(pixels.PeekInt(Int(Int(0)*4+Int(0)*320*4)))
            r = pixel[3] ; g = pixel[2] ; b = pixel[1] ; a = pixel[0]                  
            If r <> checkred Or g <> checkgreen Or  b <> checkblue Or a <> checkalpha Then sprite.SetColor 0,0,0,1 ; sprite.DrawRect xxx,yyy,1,1
            If r = checkred And g = checkgreen And b = checkblue And a = checkalpha Then sprite.SetColor 0,1,0,1 ; sprite.DrawRect xxx,yyy,1,1 ' Green if okay
        Next

        sprite.Flush ; canvas.SetColor 1,1,1,1 ; canvas.DrawRect 0,0,640,400,screen2,0,0,320,200      
         canvas.Flush
         Return 0
    End  
   
    Function ColorToRgb:Int[](value:Int)
   
        ' If value < 0 Then Print "negative"
       
            Local resp:Int[] ;  resp = resp.Resize(4)
            Local v:Int = value ;  resp[3] = v & 255 ; v = v Shr 8 ; resp[2] = v & 255 ; v = v Shr 8 ;  resp[1] = v & 255 ; v = v Shr 8 ;  resp[0] = v & 255
            Return resp
    End  
   
End
 

Wingnut

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,284
One thing I forgot to emphasize, when I say going back and forth between floats(the normalised form) and integers is okay, I mean that you should NEVER ever manipulate the floats, you just use them as a place in memory. Don't assume that you can half them for instance to get half the brightness, instead convert it to byte first and THEN begin to do what you want.

Some things might very well work but if you want to keep it simple, never do it.
 

Wingnut

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,284
I can also assure that Cerberus-x is perfect in handling the bits cross-platform, there is asbolutely no problem or difference in the outputs, weather you spit out html5, android, macos, windows or linux. There's the consistent quality in that you will get the same.

Which makes Cerberus into an amazing and reliable tool.

I have not tried ios yet but I see no reason why it should be different and unique.
You never could get this far with say Monkey etc because of all the bugs that was in the way.
Mike fixed most if not all of them this last year.
 
Last edited:

Wingnut

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,284
Remember that the example is all about the idea how to handle things and not about the READ/WRITE pixels command.
You should never read a pixel at a time, you set the width and height and read everything in one go always.

When you program for macos, windows, html5, and Linux, there will be an okay penalty vs size, and you can get away with nice sizes until you fall off 60fps. Android on the other hand, will punish you quickly, so keep it around 1x1 - ca 320x200, the smaller the better otherwise you might get < 60 fps on lower-end Android devices. Android will not just dump it to 30 fps it will give you freezes and crashes, so it's very important to keep your speed.
 
Top Bottom