About Monkey 2 › Forums › Monkey 2 Code Library › Color extensions
This topic contains 4 replies, has 3 voices, and was last updated by
nerobot 1 year, 4 months ago.
Viewing 5 posts - 1 through 5 (of 5 total)
-
AuthorPosts
-
December 5, 2017 at 8:29 am #12138
Let’s put all useful color extensions in one place.
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172Struct Color Extension' Returns Color from hex-string.' Hex can be: #RGB / #ARGB / #RRGGBB / #AARRGGBB' Returns Magenta if format is incorrect'Function FromHex:Color( hex:String )Local str:=hexLocal a:=1.0,r:=0.0,g:=0.0,b:=0.0If str.Length=4 '#RGBr=StringToULong( str.Slice( 1,2 ),16 )/15.0g=StringToULong( str.Slice( 2,3 ),16 )/15.0b=StringToULong( str.Slice( 3,4 ),16 )/15.0Else If str.Length=5 '#ARGBa=StringToULong( str.Slice( 1,2 ),16 )/15.0r=StringToULong( str.Slice( 2,3 ),16 )/15.0g=StringToULong( str.Slice( 3,4 ),16 )/15.0b=StringToULong( str.Slice( 4,5 ),16 )/15.0Else If str.Length=7 '#RRGGBBr=StringToULong( str.Slice( 1,3 ),16 )/255.0g=StringToULong( str.Slice( 3,5 ),16 )/255.0b=StringToULong( str.Slice( 5,7 ),16 )/255.0Else If str.Length=9 '#AARRGGBBa=StringToULong( str.Slice( 1,3 ),16 )/255.0r=StringToULong( str.Slice( 3,5 ),16 )/255.0g=StringToULong( str.Slice( 5,7 ),16 )/255.0b=StringToULong( str.Slice( 7,9 ),16 )/255.0ElseReturn Color.MagentaEndifReturn New Color( r,g,b,a )End' Returns #AARRGGBB string.'Method ToHex:String()Local aa:=ULongToString( a*255,16 )If aa.Length<2 Then aa="0"+aaLocal rr:=ULongToString( r*255,16 )If rr.Length<2 Then rr="0"+rrLocal gg:=ULongToString( g*255,16 )If gg.Length<2 Then gg="0"+ggLocal bb:=ULongToString( b*255,16 )If bb.Length<2 Then bb="0"+bbReturn "#"+aa+rr+gg+bbEnd' Returns array with [r,g,b,a] values.'Method ToJson:JsonValue()Return New JsonArray( New JsonValue[]( New JsonNumber( r ),New JsonNumber( g ),New JsonNumber( b ),New JsonNumber( a ) ) )End' Return Color extracted from array or from string (in hex format).'Function FromJson:Color( json:JsonValue )If json.IsArrayLocal arr:=json.ToArray()Return New Color( arr[0].ToNumber(),arr[1].ToNumber(),arr[2].ToNumber(),arr[3].ToNumber() )ElseReturn FromHex( json.ToString() )EndifEndEndDecember 5, 2017 at 8:41 am #12139If you want to make color lighter or darker, you can multiply it with float:
Local darkerPink:=Color.Pink * .8
Local lighter:=Color.FromHex("#554488") * 1.2
December 6, 2017 at 9:56 am #12171It’s a shame you can’t use ‘New’ with extensions.
I’d love to haveMonkey12345678910Struct Color ExtensionMethod New( r:UByte,g:UByte,b:UByte,a:UByte=255 )Self.r=r/255.0Self.g=g/255.0Self.b=b/255.0Self.a=a/255.0EndEndOh well, you can still do:
Monkey1234567Struct Color ExtensionFunction FromByte:Color( r:UByte,g:UByte,b:UByte,a:UByte=255 )Return New Color( r/255.0,g/255.0,b/255.0,a/255.0 )EndEndDecember 6, 2017 at 4:18 pm #12179Some updates to the code as nerobot suggested.
Monkey1234567891011121314151617181920212223#Import "<std>"Using std.graphicsFunction Main()Local colorSample:UInt = 4294902015Local colorPink := Color.FromARGB(colorSample)Print(colorPink)Local uintPink := Color.ToARGB(colorPink)Print(uintPink)If colorSample = uintPinkPrint("Success")EndEndStruct Color ExtensionFunction ToARGB:UInt(color:Color)Return UInt(color.A * 255) Shl 24 | UInt(color.R * 255) Shl 16 | UInt(color.G * 255) Shl 8 | UInt(color.B * 255)EndEndDecember 7, 2017 at 1:24 am #12185Code is not mine but good to have it here.:)
And it’s ABGR format not RGBA. So we must rename that function to be correct.
-
AuthorPosts
Viewing 5 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic.