color.monkey2 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. Namespace std.graphics
  2. #rem monkeydoc The Color type provides support for manipulating red, green blue, alpha colors.
  3. #end
  4. Struct Color
  5. #rem monkeydoc Transparent black.
  6. #end
  7. Const None:=New Color( 0,0,0,0 )
  8. #rem monkeydoc Red.
  9. #end
  10. Const Red:=New Color( 1,0,0 )
  11. #rem monkeydoc Green.
  12. #end
  13. Const Green:=New Color( 0,1,0 )
  14. #rem monkeydoc Blue.
  15. #end
  16. Const Blue:=New Color( 0,0,1 )
  17. #rem monkeydoc Yellow.
  18. #end
  19. Const Yellow:=New Color( 1,1,0 )
  20. #rem monkeydoc Magenta.
  21. #end
  22. Const Magenta:=New Color( 1,0,1 )
  23. #rem monkeydoc Cyan.
  24. #end
  25. Const Cyan:=New Color( 0,1,1 )
  26. #rem monkeydoc Black.
  27. #end
  28. Const Black:=New Color( 0,0,0 )
  29. #rem monkeydoc White.
  30. #end
  31. Const White:=New Color( 1,1,1 )
  32. #rem monkeydoc Grey.
  33. #end
  34. Const Grey:=New Color( .5,.5,.5 )
  35. #rem monkeydoc Light Grey.
  36. #end
  37. Const LightGrey:=New Color( .75,.75,.75 )
  38. #rem monkeydoc Dark Grey.
  39. #end
  40. Const DarkGrey:=New Color( .25,.25,.25 )
  41. #rem monkeydoc Red component of color.
  42. #end
  43. Field r:Float
  44. #rem monkeydoc Green component of color.
  45. #end
  46. Field g:Float
  47. #rem monkeydoc Blue component of color.
  48. #end
  49. Field b:Float
  50. #rem monkeydoc Alpha component of color.
  51. #end
  52. Field a:Float
  53. #rem monkeydoc Creates a new color.
  54. #end
  55. Method New( a:Float=1 )
  56. Self.a=a
  57. End
  58. Method New( i:Float,a:Float=1 )
  59. Self.r=i
  60. Self.g=i
  61. Self.b=i
  62. Self.a=1
  63. End
  64. Method New( r:Float,g:Float,b:Float,a:Float=1 )
  65. Self.r=r
  66. Self.g=g
  67. Self.b=b
  68. Self.a=a
  69. End
  70. #rem monkeydoc Converts the color to printable string.
  71. #end
  72. Operator To:String()
  73. Return "Color("+r+","+g+","+b+","+a+")"
  74. End
  75. #rem monkeydoc The Red color component.
  76. #end
  77. Property R:Float()
  78. Return r
  79. Setter( r:Float )
  80. Self.r=r
  81. End
  82. #rem monkeydoc The green color component.
  83. #end
  84. Property G:Float()
  85. Return g
  86. Setter( g:Float )
  87. Self.g=g
  88. End
  89. #rem monkeydoc The blue color component.
  90. #end
  91. Property B:Float()
  92. Return b
  93. Setter( b:Float )
  94. Self.b=b
  95. End
  96. #rem monkeydoc The alpha color component.
  97. #end
  98. Property A:Float()
  99. Return a
  100. Setter( a:Float )
  101. Self.a=a
  102. End
  103. #rem monkeydoc Multiplies the color by another color or value and returns the result.
  104. #end
  105. Operator*:Color( color:Color )
  106. Return New Color( r*color.r,g*color.g,b*color.b,a*color.a )
  107. End
  108. Operator*:Color( scale:Float )
  109. Return New Color( r*scale,g*scale,b*scale,a*scale )
  110. End
  111. #rem monkeydoc Divides the color by another color or value and returns the result.
  112. #end
  113. Operator/:Color( color:Color )
  114. Return New Color( r/color.r,g/color.g,b/color.b,a/color.a )
  115. End
  116. Operator/:Color( scale:Float )
  117. Return New Color( r/scale,g/scale,b/scale,a/scale )
  118. End
  119. #rem monkeydoc Adds another color or value to the color and returns the result.
  120. #end
  121. Operator+:Color( color:Color )
  122. Return New Color( r+color.r,g+color.g,b+color.b,a+color.a )
  123. End
  124. Operator+:Color( offset:Float )
  125. Return New Color( r+offset,g+offset,b+offset,a+offset )
  126. End
  127. #rem monkeydoc Subtracts another color or value from the color and returns the result.
  128. #end
  129. Operator-:Color( color:Color )
  130. Return New Color( r-color.r,g-color.g,b-color.b,a-color.a )
  131. End
  132. Operator-:Color( offset:Float )
  133. Return New Color( r-offset,g-offset,b-offset,a-offset )
  134. End
  135. #rem monkeydoc Blends the color with another color and returns the result.
  136. #end
  137. Method Blend:Color( color:Color,delta:Float )
  138. Local idelta:=1-delta
  139. Return New Color( r*idelta+color.r*delta,g*idelta+color.g*delta,b*idelta+color.b*delta,a*idelta+color.a*delta )
  140. End
  141. #rem monkeydoc Converts the color to 32 bit ARGB format.
  142. #end
  143. Method ToARGB:UInt()
  144. Return UInt(a*255) Shl 24 | UInt(r*255) Shl 16 | UInt(g*255) Shl 8 | UInt(b*255)
  145. End
  146. #rem monkeydoc Converts the color to printable string.
  147. #end
  148. Method ToString:String()
  149. Return Self
  150. End
  151. #rem monkeydoc Creates a color from hue, saturation and value.
  152. #end
  153. Function FromHSV:Color( h:Float,s:Float,v:Float,a:Float=1 )
  154. h*=6
  155. Local f:=h-Floor( h )
  156. Local p:=v * ( 1 - s )
  157. Local q:=v * ( 1 - ( s * f ) )
  158. Local t:=v * ( 1 - ( s * ( 1-f ) ) )
  159. Local r:Float,g:Float,b:Float
  160. Select Int( h ) Mod 6
  161. Case 0 r=v ; g=t ; b=p
  162. Case 1 r=q ; g=v ; b=p
  163. Case 2 r=p ; g=v ; b=t
  164. Case 3 r=p ; g=q ; b=v
  165. Case 4 r=t ; g=p ; b=v
  166. Case 5 r=v ; g=p ; b=q
  167. End
  168. Return New Color( r,g,b,a )
  169. End
  170. #rem monkeydoc Creates a color from a 32 bit ARGB color.
  171. #end
  172. Function FromARGB:Color( argb:UInt )
  173. Local a:=(argb Shr 24 & $ff)/255.0
  174. Local r:=(argb Shr 16 & $ff)/255.0
  175. Local g:=(argb Shr 8 & $ff)/255.0
  176. Local b:=(argb & $ff)/255.0
  177. Return New Color( r,g,b,a )
  178. End
  179. End