color.monkey2 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 The Red color component.
  71. #end
  72. Property R:Float()
  73. Return r
  74. Setter( r:Float )
  75. Self.r=r
  76. End
  77. #rem monkeydoc The green color component.
  78. #end
  79. Property G:Float()
  80. Return g
  81. Setter( g:Float )
  82. Self.g=g
  83. End
  84. #rem monkeydoc The blue color component.
  85. #end
  86. Property B:Float()
  87. Return b
  88. Setter( b:Float )
  89. Self.b=b
  90. End
  91. #rem monkeydoc The alpha color component.
  92. #end
  93. Property A:Float()
  94. Return a
  95. Setter( a:Float )
  96. Self.a=a
  97. End
  98. #rem monkeydoc Multiplies the color by another color or value and returns the result.
  99. #end
  100. Operator*:Color( color:Color )
  101. Return New Color( r*color.r,g*color.g,b*color.b,a*color.a )
  102. End
  103. Operator*:Color( scale:Float )
  104. Return New Color( r*scale,g*scale,b*scale,a*scale )
  105. End
  106. #rem monkeydoc Divides the color by another color or value and returns the result.
  107. #end
  108. Operator/:Color( color:Color )
  109. Return New Color( r/color.r,g/color.g,b/color.b,a/color.a )
  110. End
  111. Operator/:Color( scale:Float )
  112. Return New Color( r/scale,g/scale,b/scale,a/scale )
  113. End
  114. #rem monkeydoc Adds another color or value to the color and returns the result.
  115. #end
  116. Operator+:Color( color:Color )
  117. Return New Color( r+color.r,g+color.g,b+color.b,a+color.a )
  118. End
  119. Operator+:Color( offset:Float )
  120. Return New Color( r+offset,g+offset,b+offset,a+offset )
  121. End
  122. #rem monkeydoc Subtracts another color or value from the color and returns the result.
  123. #end
  124. Operator-:Color( color:Color )
  125. Return New Color( r-color.r,g-color.g,b-color.b,a-color.a )
  126. End
  127. Operator-:Color( offset:Float )
  128. Return New Color( r-offset,g-offset,b-offset,a-offset )
  129. End
  130. #rem monkeydoc Blends the color with another color and returns the result.
  131. #end
  132. Method Blend:Color( color:Color,delta:Float )
  133. Local idelta:=1-delta
  134. Return New Color( r*idelta+color.r*delta,g*idelta+color.g*delta,b*idelta+color.b*delta,a*idelta+color.a*delta )
  135. End
  136. #rem monkeydoc Converts the color to 32 bit ARGB format.
  137. #end
  138. Method ToARGB:UInt()
  139. Return UInt(a*255) Shl 24 | UInt(r*255) Shl 16 | UInt(g*255) Shl 8 | UInt(b*255)
  140. End
  141. #rem monkeydoc Converts the color to printable string.
  142. #end
  143. Method ToString:String()
  144. Return "Color("+r+","+g+","+b+","+a+")"
  145. End
  146. #rem monkeydoc Creates a color from hue, saturation and value.
  147. #end
  148. Function FromHSV:Color( h:Float,s:Float,v:Float,a:Float=1 )
  149. h*=6
  150. Local f:=h-Floor( h )
  151. Local p:=v * ( 1 - s )
  152. Local q:=v * ( 1 - ( s * f ) )
  153. Local t:=v * ( 1 - ( s * ( 1-f ) ) )
  154. Local r:Float,g:Float,b:Float
  155. Select Int( h ) Mod 6
  156. Case 0 r=v ; g=t ; b=p
  157. Case 1 r=q ; g=v ; b=p
  158. Case 2 r=p ; g=v ; b=t
  159. Case 3 r=p ; g=q ; b=v
  160. Case 4 r=t ; g=p ; b=v
  161. Case 5 r=v ; g=p ; b=q
  162. End
  163. Return New Color( r,g,b,a )
  164. End
  165. #rem monkeydoc Creates a color from a 32 bit ARGB color.
  166. #end
  167. Function FromARGB:Color( argb:UInt )
  168. Local a:=(argb Shr 24 & $ff)/255.0
  169. Local r:=(argb Shr 16 & $ff)/255.0
  170. Local g:=(argb Shr 8 & $ff)/255.0
  171. Local b:=(argb & $ff)/255.0
  172. Return New Color( r,g,b,a )
  173. End
  174. End