color.monkey2 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. Namespace std.graphics
  2. using std.geom
  3. #rem monkeydoc The Color type provides support for manipulating red, green blue, alpha colors.
  4. #end
  5. Struct Color
  6. #rem monkeydoc Transparent black.
  7. #end
  8. Const None:=New Color( 0,0,0,0 )
  9. #rem monkeydoc Black. Equivalent to New Color( 0,0,0 ).
  10. #end
  11. Const Black:=New Color( 0,0,0 )
  12. #rem monkeydoc Grey. Equivalent to New Color( .5,.5,.5 ).
  13. #end
  14. Const Grey:=New Color( .5,.5,.5 )
  15. #rem monkeydoc Light Grey. Equivalent to New Color( .75,.75,.75 ).
  16. #end
  17. Const LightGrey:=New Color( .75,.75,.75 )
  18. #rem monkeydoc Dark Grey. Equivalent to New Color( .25,.25,.25 ).
  19. #end
  20. Const DarkGrey:=New Color( .25,.25,.25 )
  21. #rem monkeydoc White. Equivalent to New Color( 1,1,1 ).
  22. #end
  23. Const White:=New Color( 1,1,1 )
  24. #rem monkeydoc Red. Equivalent to New Color( 1,0,0 ).
  25. #end
  26. Const Red:=New Color( 1,0,0 )
  27. #rem monkeydoc Green. Equivalent to New Color( 0,1,0 ).
  28. #end
  29. Const Green:=New Color( 0,1,0 )
  30. #rem monkeydoc Blue. Equivalent to New Color( 0,0,1 ).
  31. #end
  32. Const Blue:=New Color( 0,0,1 )
  33. #rem monkeydoc Brown. Equivalent to New Color( .7,.4,.1 ).
  34. #end
  35. Const Brown:=New Color( .7,.4,.1 )
  36. #rem monkeydoc Orange. Equivalent to New Color( 1,.5,0 ).
  37. #end
  38. Const Orange:=New Color( 1,.5,0 )
  39. #rem monkeydoc Yellow. Equivalent to New Color( 1,1,0 ).
  40. #end
  41. Const Yellow:=New Color( 1,1,0 )
  42. #rem monkeydoc Lime. Equivalent to New Color( .7,1,0 ).
  43. #end
  44. Const Lime:=New Color( .7,1,0 )
  45. #rem monkeydoc Pine. Equivalent to New Color( 0,.5,0 ).
  46. #end
  47. Const Pine:=New Color( 0,.5,0 )
  48. #rem monkeydoc Aqua. Equivalent to New Color( 0,.9,.4 ).
  49. #end
  50. Const Aqua:=New Color( 0,.9,.4 )
  51. #rem monkeydoc Cyan. Equivalent to New Color( 0,1,1 ).
  52. #end
  53. Const Cyan:=New Color( 0,1,1 )
  54. #rem monkeydoc Sky. Equivalent to New Color( 0,.5,1 ).
  55. #end
  56. Const Sky:=New Color( 0,.5,1 )
  57. #rem monkeydoc Steel. Equivalent to New Color( .2,.2,.7 ).
  58. #end
  59. Const Steel:=New Color( .2,.2,.7 )
  60. #rem monkeydoc Violet. Equivalent to New Color( .7,0,1 ).
  61. #end
  62. Const Violet:=New Color( .7,0,1 )
  63. #rem monkeydoc Magenta. Equivalent to New Color( 1,0,1 ).
  64. #end
  65. Const Magenta:=New Color( 1,0,1 )
  66. #rem monkeydoc Puce. Equivalent to New Color( 1,0,.4 ).
  67. #end
  68. Const Puce:=New Color( 1,0,.4 )
  69. #rem monkeydoc Skin. Equivalent to New Color( .8,.5,.6 ).
  70. #end
  71. Const Skin:=New Color( .8,.5,.6 )
  72. #rem monkeydoc Pink. Equivalent to New Color( 1,.75,.8 ).
  73. #end
  74. Const Pink:=New Color( 1,.75,.8 )
  75. #rem monkeydoc HotPink. Equivalent to New Color( 1,.41,.71 ).
  76. #end
  77. Const HotPink:=New Color( 1,.41,.71 )
  78. #rem monkeydoc SeaGreen. Roughly equivalent to New Color( .03,.3,.25 ).
  79. #end
  80. Const SeaGreen:=New Color( .031372,.301960,.247058,1 )
  81. #rem monkeydoc Silver. Roughly equivalent to New Color( .99,.98,.96 ).
  82. #end
  83. Const Silver:=New Color( 0.98695202723239916,0.98157612499486091,0.96058105436290453 )
  84. #rem monkeydoc Aluminum. Roughly equivalent to New Color( .96,.96,.96 ).
  85. #end
  86. Const Aluminum:=New Color( 0.95955910300613745,0.9635188914336692,0.96495768667887971 )
  87. #rem monkeydoc Gold. Roughly equivalent to New Color( 1,.89,.61 ).
  88. #end
  89. Const Gold:=New Color( 1,0.88565078560356991,0.6091625017721024 )
  90. #rem monkeydoc Copper. Roughly equivalent to New Color( .98,.82,.76 ).
  91. #end
  92. Const Copper:=New Color( 0.9792921449434141,0.81490079942355442,0.75455014940288267 )
  93. #rem monkeydoc Chromium. Roughly equivalent to New Color( .76,.77,.77 ).
  94. #end
  95. Const Chromium:=New Color( 0.76178782381338439,0.76588820797089974,0.76472402871006473 )
  96. #rem monkeydoc Nickel. Roughly equivalent to New Color( .83,.8,.75 ).
  97. #end
  98. Const Nickel:=New Color( 0.827766413700323,0.79798492878548577,0.74652364685504802 )
  99. #rem monkeydoc Titanium. Roughly equivalent to New Color( .76,.73,.7 ).
  100. #end
  101. Const Titanium:=New Color( 0.75694694835172049,0.72760746687141564,0.69520723368860826 )
  102. #rem monkeydoc Cobalt. Roughly equivalent to New Color( .83,.82,.81 ).
  103. #end
  104. Const Cobalt:=New Color( 0.82910355988659823,0.82495893307721546,0.81275025476652396 )
  105. #rem monkeydoc Platinum. Roughly equivalent to New Color( .84,.82,.78 ).
  106. #end
  107. Const Platinum:=New Color( 0.83493408973507777,0.81484503266020314,0.78399912482207756 )
  108. #rem monkeydoc Red component of color.
  109. #end
  110. Field r:Float
  111. #rem monkeydoc Green component of color.
  112. #end
  113. Field g:Float
  114. #rem monkeydoc Blue component of color.
  115. #end
  116. Field b:Float
  117. #rem monkeydoc Alpha component of color.
  118. #end
  119. Field a:Float
  120. #rem monkeydoc Creates a new color.
  121. #end
  122. Method New( a:Float=1 )
  123. Self.a=a
  124. End
  125. Method New( i:Float,a:Float=1 )
  126. Self.r=i
  127. Self.g=i
  128. Self.b=i
  129. Self.a=a
  130. End
  131. Method New( r:Float,g:Float,b:Float,a:Float=1 )
  132. Self.r=r
  133. Self.g=g
  134. Self.b=b
  135. Self.a=a
  136. End
  137. Method New( v:geom.Vec4f )
  138. Self.r=v.x
  139. Self.g=v.y
  140. Self.b=v.z
  141. Self.a=v.w
  142. End
  143. #rem monkeydoc Converts the color to a printable string.
  144. #end
  145. Operator To:String()
  146. Return "Color("+r+","+g+","+b+","+a+")"
  147. End
  148. #rem monkeydoc Converts the color to float 4 vector.
  149. #end
  150. Operator To:geom.Vec4f()
  151. Return New Vec4f( r,g,b,a )
  152. End
  153. #rem monkeydoc The Red color component.
  154. #end
  155. Property R:Float()
  156. Return r
  157. Setter( r:Float )
  158. Self.r=r
  159. End
  160. #rem monkeydoc The green color component.
  161. #end
  162. Property G:Float()
  163. Return g
  164. Setter( g:Float )
  165. Self.g=g
  166. End
  167. #rem monkeydoc The blue color component.
  168. #end
  169. Property B:Float()
  170. Return b
  171. Setter( b:Float )
  172. Self.b=b
  173. End
  174. #rem monkeydoc The alpha color component.
  175. #end
  176. Property A:Float()
  177. Return a
  178. Setter( a:Float )
  179. Self.a=a
  180. End
  181. #rem monkeydoc Multiplies the color by another color or value and returns the result.
  182. #end
  183. Operator*:Color( color:Color )
  184. Return New Color( r*color.r,g*color.g,b*color.b,a*color.a )
  185. End
  186. Operator*:Color( scale:Float )
  187. Return New Color( r*scale,g*scale,b*scale,a*scale )
  188. End
  189. #rem monkeydoc Divides the color by another color or value and returns the result.
  190. #end
  191. Operator/:Color( color:Color )
  192. Return New Color( r/color.r,g/color.g,b/color.b,a/color.a )
  193. End
  194. Operator/:Color( scale:Float )
  195. Return New Color( r/scale,g/scale,b/scale,a/scale )
  196. End
  197. #rem monkeydoc Adds another color or value to the color and returns the result.
  198. #end
  199. Operator+:Color( color:Color )
  200. Return New Color( r+color.r,g+color.g,b+color.b,a+color.a )
  201. End
  202. Operator+:Color( offset:Float )
  203. Return New Color( r+offset,g+offset,b+offset,a+offset )
  204. End
  205. #rem monkeydoc Subtracts another color or value from the color and returns the result.
  206. #end
  207. Operator-:Color( color:Color )
  208. Return New Color( r-color.r,g-color.g,b-color.b,a-color.a )
  209. End
  210. Operator-:Color( offset:Float )
  211. Return New Color( r-offset,g-offset,b-offset,a-offset )
  212. End
  213. #rem monkeydoc Blends the color with another color and returns the result.
  214. #end
  215. Method Blend:Color( color:Color,delta:Float )
  216. Local idelta:=1-delta
  217. Return New Color( r*idelta+color.r*delta,g*idelta+color.g*delta,b*idelta+color.b*delta,a*idelta+color.a*delta )
  218. End
  219. #rem monkeydoc Converts the color to 32 bit big endian ARGB format.
  220. Big endian ARGB is the same as little endian BGRA.
  221. #end
  222. Method ToARGB:UInt()
  223. Return UInt(a*255) Shl 24 | UInt(r*255) Shl 16 | UInt(g*255) Shl 8 | UInt(b*255)
  224. End
  225. #rem monkeydoc Converts the color to 32 bit big endian BGRA format.
  226. Big endian BGRA is the same as little endian ARGB.
  227. #end
  228. Method ToBGRA:UInt()
  229. Return UInt(b*255) Shl 24 | UInt(g*255) Shl 16 | UInt(r*255) Shl 8 | UInt(a*255)
  230. End
  231. #rem monkeydoc Converts the color to 32 bit big endian RGBA format.
  232. Big endian RGBA is the same as little endian ABGR.
  233. #end
  234. Method ToRGBA:UInt()
  235. Return UInt(r*255) Shl 24 | UInt(g*255) Shl 16 | UInt(b*255) Shl 8 | UInt(a*255)
  236. End
  237. #rem monkeydoc Converts the color to 32 bit big endianABGR format.
  238. Big endian ABGR is the same as little endian RGBA.
  239. #end
  240. Method ToABGR:UInt()
  241. Return UInt(a*255) Shl 24 | UInt(b*255) Shl 16 | UInt(g*255) Shl 8 | UInt(r*255)
  242. End
  243. #rem monkeydoc Converts the color to printable string.
  244. #end
  245. Method ToString:String()
  246. Return Self
  247. End
  248. #rem monkeydoc Creates a color from hue, saturation and value.
  249. #end
  250. Function FromHSV:Color( h:Float,s:Float,v:Float,a:Float=1 )
  251. h*=6
  252. Local f:=h-Floor( h )
  253. Local p:=v * ( 1 - s )
  254. Local q:=v * ( 1 - ( s * f ) )
  255. Local t:=v * ( 1 - ( s * ( 1-f ) ) )
  256. Local r:Float,g:Float,b:Float
  257. Select Int( h ) Mod 6
  258. Case 0 r=v ; g=t ; b=p
  259. Case 1 r=q ; g=v ; b=p
  260. Case 2 r=p ; g=v ; b=t
  261. Case 3 r=p ; g=q ; b=v
  262. Case 4 r=t ; g=p ; b=v
  263. Case 5 r=v ; g=p ; b=q
  264. End
  265. Return New Color( r,g,b,a )
  266. End
  267. #rem monkeydoc Creates a color from a 32 bit big endian ARGB color.
  268. Big endian ARGB is the same as little endian BGRA.
  269. #end
  270. Function FromARGB:Color( argb:UInt )
  271. Local a:=(argb Shr 24 & $ff)/255.0
  272. Local r:=(argb Shr 16 & $ff)/255.0
  273. Local g:=(argb Shr 8 & $ff)/255.0
  274. Local b:=(argb & $ff)/255.0
  275. Return New Color( r,g,b,a )
  276. End
  277. #rem monkeydoc Creates a color from a 32 bit big endian BGRA color.
  278. Big endian BGRA is the same as little endian ARGB.
  279. #end
  280. Function FromBGRA:Color( bgra:UInt )
  281. Local b:=(bgra Shr 24 & $ff)/255.0
  282. Local g:=(bgra Shr 16 & $ff)/255.0
  283. Local r:=(bgra Shr 8 & $ff)/255.0
  284. Local a:=(bgra & $ff)/255.0
  285. Return New Color( r,g,b,a )
  286. End
  287. #rem monkeydoc Creates a color from a 32 bit big endian RGBA color.
  288. Big endian RGBA is the same as little endian ABGR.
  289. #end
  290. Function FromRGBA:Color( rgba:UInt )
  291. Local r:=(rgba Shr 24 & $ff)/255.0
  292. Local g:=(rgba Shr 16 & $ff)/255.0
  293. Local b:=(rgba Shr 8 & $ff)/255.0
  294. Local a:=(rgba & $ff)/255.0
  295. Return New Color( r,g,b,a )
  296. End
  297. #rem monkeydoc Creates a color from a 32 bit big endian ABGR color.
  298. Big endian ABGR is the same as little endian RGBA.
  299. #end
  300. Function FromABGR:Color( abgr:UInt )
  301. Local a:=(abgr Shr 24 & $ff)/255.0
  302. Local b:=(abgr Shr 16 & $ff)/255.0
  303. Local g:=(abgr Shr 8 & $ff)/255.0
  304. Local r:=(abgr & $ff)/255.0
  305. Return New Color( r,g,b,a )
  306. End
  307. #rem monkeydoc Creates a random color.
  308. #end
  309. Function Rnd:Color()
  310. Return FromHSV( random.Rnd(6),1,1 )
  311. End
  312. End