LinearGradientBrush.jvm.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. using System;
  2. using java.awt;
  3. namespace System.Drawing.Drawing2D
  4. {
  5. /// <summary>
  6. /// Summary description for LinearGradientBrush.
  7. /// </summary>
  8. public sealed class LinearGradientBrush : Brush
  9. {
  10. //TODO:this 3 entries nowbody need
  11. GradientPaint _nativeObject;
  12. protected override Paint NativeObject {
  13. get {
  14. return _nativeObject;
  15. }
  16. }
  17. //
  18. Matrix _transform;
  19. WrapMode _wrapmode;
  20. ColorBlend _colorblend;
  21. Blend _blend;
  22. float _focus = 0f;
  23. float _scale = 0f;
  24. float x1,y1,x2,y2;
  25. Matrix _internalTransform;
  26. LinearGradientMode _linMode = LinearGradientMode.Horizontal;
  27. #if DEBUG_GRADIENT_BRUSH
  28. public PointF [] dPoints;
  29. #endif
  30. /*
  31. internal LinearGradientBrush (GradientPaint native) : base (native)
  32. {
  33. _transform = new Matrix();
  34. _wrapmode = WrapMode.Clamp;
  35. }
  36. */
  37. internal LinearGradientBrush (float x1,float y1, Color color1,float x2, float y2, Color color2,float angle)
  38. {
  39. this.x1=x1;
  40. this.x2=x2;
  41. this.y1=y1;
  42. this.y2=y2;
  43. _internalTransform = new Matrix();
  44. _internalTransform.RotateAt(-angle,new PointF(x2-x1,y2-y1));
  45. PointF [] pts = new PointF[2];
  46. //default to gorizontal vector
  47. pts[0] = new PointF(x1,y2-y1);
  48. pts[1] = new PointF(x2,y2-y1);
  49. _internalTransform.TransformPoints(pts);
  50. _nativeObject = new GradientPaint( pts[0].X,pts[0].Y,
  51. new java.awt.Color(color1.R,color1.G,color1.B,color1.A),
  52. pts[1].X,pts[1].Y,
  53. new java.awt.Color(color2.R,color2.G,color2.B,color2.A));
  54. }
  55. internal PointF [] GetMedianeEnclosingRect(float x1,float y1,float x2,float y2)
  56. {
  57. //get hypotenuse length
  58. double hipLength = Math.Sqrt(Math.Pow(y2-y1,2)+Math.Pow(x2-x1,2));
  59. //mark linear point on the rectangle left edge: we will rotate
  60. //it from there ...
  61. double yy = y1+hipLength;
  62. //... by this angle
  63. double rotationAngle = Math.Atan2(x2-x1,y2-y1);
  64. float xxSemiWidth = (float)((x2-x1)*Math.Cos(rotationAngle));
  65. PointF rotationPoint=new PointF(x1,y1);
  66. Matrix mat = new Matrix();
  67. //here the magic
  68. mat.RotateAt(-(float)(rotationAngle/Math.PI*180),rotationPoint);
  69. PointF []points = new PointF[]{ new PointF(x1-xxSemiWidth,y1),
  70. new PointF(x1+xxSemiWidth,y1),
  71. new PointF(x1+xxSemiWidth,(float)yy),
  72. new PointF(x1-xxSemiWidth,(float)yy)
  73. };
  74. mat.TransformPoints(points);
  75. //now we have original rectangle withing another (enclosing)
  76. //rect that is reclined to left
  77. //the central points of new rectangle sides are exactly
  78. //the starting points of gradient in BackwardDiagonal
  79. //case. For ForwardDiagonal you can use the same points with
  80. //"y" or "x" coordinates swapped
  81. #if DEBUG_GRADIENT_BRUSH
  82. dPoints = points;
  83. #endif
  84. return points;
  85. }
  86. internal PointF[] GetDiagStartStopCoords(PointF []points)
  87. {
  88. return new PointF[]{
  89. new PointF((points[3].X-points[0].X)/2+points[0].X,
  90. (points[2].Y-points[1].Y)/2+points[1].Y),
  91. new PointF((points[2].X-points[1].X)/2+points[1].X,
  92. (points[3].Y-points[0].Y)/2+points[0].Y)};
  93. }
  94. internal void Init(float x1,float y1, Color color1,float x2, float y2, Color color2, LinearGradientMode mode, bool cycle)
  95. {
  96. float xx1;
  97. float yy1;
  98. float xx2;
  99. float yy2;
  100. if(mode == LinearGradientMode.Vertical)
  101. {
  102. xx1 = x2-x1;
  103. yy1 = y1;
  104. xx2 = x2-x1;
  105. yy2 = y2;
  106. }
  107. else if(mode == LinearGradientMode.Horizontal)
  108. {
  109. xx1 = x1;
  110. yy1 = y2-y1;
  111. xx2 = x2;
  112. yy2 = y2-y1;
  113. }
  114. else if(mode == LinearGradientMode.ForwardDiagonal)
  115. {
  116. if(y2-y1 != x2-x1)
  117. {
  118. PointF [] pts = GetDiagStartStopCoords(GetMedianeEnclosingRect(x1,y1,x2,y2));
  119. xx1 = pts[0].X;
  120. yy1 = pts[0].Y;
  121. xx2 = pts[1].X;
  122. yy2 = pts[1].Y;
  123. }
  124. else
  125. {
  126. xx1 = x1;
  127. yy1 = y1;
  128. xx2 = x2;
  129. yy2 = y2;
  130. }
  131. }
  132. else /*if(mode == LinearGradientMode.BackwardDiagonal)*/
  133. {
  134. if(y2-y1 != x2-x1)
  135. {
  136. PointF [] pts = GetDiagStartStopCoords(GetMedianeEnclosingRect(x1,y1,x2,y2));
  137. xx1 = pts[1].X;
  138. yy1 = pts[0].Y;
  139. xx2 = pts[0].X;
  140. yy2 = pts[1].Y;
  141. }
  142. else
  143. {
  144. xx1 = x2;
  145. yy1 = y1;
  146. xx2 = x1;
  147. yy2 = y2;
  148. }
  149. }
  150. //move this into upper if's for better perfomance
  151. if(cycle)
  152. {
  153. if(xx2>xx1)
  154. xx2 = (xx2-xx1)/2+xx1;
  155. else
  156. xx2 = (xx1-xx2)/2+xx2;
  157. if(yy2>yy1)
  158. yy2 = (yy2-yy1)/2+yy1;
  159. else
  160. yy2 = (yy1-yy2)/2+yy2;
  161. }
  162. _nativeObject = new GradientPaint(xx1,yy1,
  163. new java.awt.Color(color1.R,color1.G,color1.B,color1.A),
  164. xx2,yy2,
  165. new java.awt.Color(color2.R,color2.G,color2.B,color2.A),cycle);
  166. }
  167. internal LinearGradientBrush (float x1,float y1, Color color1,float x2, float y2, Color color2, LinearGradientMode mode)
  168. {
  169. this.x1=x1;
  170. this.x2=x2;
  171. this.y1=y1;
  172. this.y2=y2;
  173. _linMode = mode;
  174. Init(x1,y1,color1,x2,y2,color2,mode,false);
  175. }
  176. public LinearGradientBrush (Point point1, Point point2, Color color1, Color color2):
  177. this((float)point1.X,(float)point1.Y,
  178. color1,
  179. (float)point2.X,(float)point2.Y,
  180. color2,LinearGradientMode.Horizontal)
  181. {
  182. }
  183. public LinearGradientBrush (PointF point1, PointF point2, Color color1, Color color2):
  184. this(point1.X,point1.Y,
  185. color1,
  186. point2.X,point2.Y,
  187. color2,LinearGradientMode.Horizontal)
  188. {
  189. }
  190. public LinearGradientBrush (Rectangle rect, Color color1, Color color2, LinearGradientMode linearGradientMode):
  191. this(rect.Left,rect.Top,
  192. color1,
  193. rect.Right,rect.Bottom,
  194. color2,linearGradientMode)
  195. {
  196. }
  197. public LinearGradientBrush (Rectangle rect, Color color1, Color color2, float angle):
  198. this((float)rect.Left,(float)rect.Top,
  199. color1,
  200. (float)rect.Right,(float)rect.Bottom,
  201. color2,angle)
  202. {
  203. }
  204. public LinearGradientBrush (RectangleF rect, Color color1, Color color2, LinearGradientMode linearGradientMode):
  205. this(rect.Left,rect.Top,
  206. color1,
  207. rect.Right,rect.Bottom,
  208. color2,linearGradientMode)
  209. {
  210. }
  211. public LinearGradientBrush (RectangleF rect, Color color1, Color color2, float angle) :
  212. this (rect, color1, color2, angle, false)
  213. {
  214. }
  215. public LinearGradientBrush (Rectangle rect, Color color1, Color color2, float angle, bool isAngleScaleable):
  216. this((float)rect.Left,(float)rect.Top,
  217. color1,
  218. (float)rect.Right,(float)rect.Bottom,
  219. color2,angle)
  220. {
  221. //TODO: angle, scalable
  222. }
  223. public LinearGradientBrush (RectangleF rect, Color color1, Color color2, float angle, bool isAngleScaleable):
  224. this((float)rect.Left,(float)rect.Top,
  225. color1,
  226. (float)rect.Right,(float)rect.Bottom,
  227. color2,angle)
  228. {
  229. //TODO: angle, scalable
  230. }
  231. // Public Properties
  232. public Blend Blend {
  233. get {
  234. throw new NotImplementedException();
  235. }
  236. set {
  237. throw new NotImplementedException();
  238. }
  239. }
  240. public bool GammaCorrection {
  241. get {
  242. throw new NotImplementedException();
  243. }
  244. set {
  245. throw new NotImplementedException();
  246. }
  247. }
  248. public ColorBlend InterpolationColors {
  249. get {
  250. throw new NotImplementedException();
  251. }
  252. set {
  253. throw new NotImplementedException();
  254. }
  255. }
  256. public Color [] LinearColors {
  257. get {
  258. Color [] cl = new Color[2];
  259. java.awt.Color c1 = ((GradientPaint)NativeObject).getColor1();
  260. java.awt.Color c2 = ((GradientPaint)NativeObject).getColor2();
  261. cl[0] = Color.FromArgb(c1.getAlpha(),c1.getRed(),c1.getGreen(),c1.getBlue());
  262. cl[1] = Color.FromArgb(c2.getAlpha(),c2.getRed(),c2.getGreen(),c2.getBlue());
  263. return cl;
  264. }
  265. set {
  266. RectangleF rect = this.Rectangle;
  267. _nativeObject = new GradientPaint( rect.X,rect.Y,
  268. value[0].NativeObject,
  269. rect.X + rect.Width,rect.Y + rect.Height,
  270. value[1].NativeObject);
  271. }
  272. }
  273. public RectangleF Rectangle {
  274. get {
  275. java.awt.geom.Point2D p1 = ((GradientPaint)NativeObject).getPoint1();
  276. java.awt.geom.Point2D p2 = ((GradientPaint)NativeObject).getPoint2();
  277. //return new RectangleF((float)p1.getX(),(float)p1.getY(),(float)(p2.getX()-p1.getX()),(float)(p2.getY() - p1.getY()));
  278. return new RectangleF(x1,y1,x2-x1,y2-y1);
  279. }
  280. }
  281. public Matrix Transform {
  282. get {
  283. return _transform;
  284. }
  285. set {
  286. _transform = value;
  287. }
  288. }
  289. public WrapMode WrapMode {
  290. get {
  291. return _wrapmode;
  292. }
  293. set {
  294. _wrapmode = value;
  295. }
  296. }
  297. // Public Methods
  298. public void MultiplyTransform (Matrix matrix)
  299. {
  300. MultiplyTransform (matrix, MatrixOrder.Prepend);
  301. }
  302. public void MultiplyTransform (Matrix matrix, MatrixOrder order)
  303. {
  304. _transform.Multiply(matrix,order);
  305. }
  306. public void ResetTransform ()
  307. {
  308. _transform = new Matrix();
  309. }
  310. public void RotateTransform (float angle)
  311. {
  312. RotateTransform (angle, MatrixOrder.Prepend);
  313. }
  314. public void RotateTransform (float angle, MatrixOrder order)
  315. {
  316. _transform.Rotate(angle,order);
  317. }
  318. public void ScaleTransform (float sx, float sy)
  319. {
  320. ScaleTransform (sx, sy, MatrixOrder.Prepend);
  321. }
  322. public void ScaleTransform (float sx, float sy, MatrixOrder order)
  323. {
  324. _transform.Scale(sx,sy,order);
  325. }
  326. public void SetBlendTriangularShape (float focus)
  327. {
  328. SetBlendTriangularShape (focus, 1.0F);
  329. }
  330. public void SetBlendTriangularShape (float focus, float scale)
  331. {
  332. Color []cl = LinearColors;
  333. Init(x1,y1,cl[0],x2,y2,cl[1],_linMode,true);
  334. //throw new NotImplementedException();
  335. }
  336. public void SetSigmaBellShape (float focus)
  337. {
  338. SetSigmaBellShape (focus, 1.0F);
  339. }
  340. public void SetSigmaBellShape (float focus, float scale)
  341. {
  342. throw new NotImplementedException();
  343. }
  344. public void TranslateTransform (float dx, float dy)
  345. {
  346. TranslateTransform (dx, dy, MatrixOrder.Prepend);
  347. }
  348. public void TranslateTransform (float dx, float dy, MatrixOrder order)
  349. {
  350. _transform.Translate(dx,dy,order);
  351. }
  352. public override object Clone ()
  353. {
  354. Color [] clrs = this.LinearColors;
  355. LinearGradientBrush b = new LinearGradientBrush(this.Rectangle,clrs[0],clrs[1],_linMode);
  356. b.Transform = this.Transform;
  357. b.Blend = this.Blend;
  358. return b;
  359. }
  360. }
  361. }