wxr
2020-03-05 0bdc0a135dbe31761b53f432ed34f347f0a4e36b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using CoreGraphics;
using Foundation;
using Shared.IO;
using UIKit;
 
 
namespace Shared
{
    /// <summary>
    /// 色板
    /// </summary>
    public class ColorPicker :View
    {
        MyUIImageView myUIImageView
        {
            get
            {
                return uiView as MyUIImageView;
            }
            set
            {
                uiView = value;
            }
        }
 
        /// <summary>
        /// Initializes a new instance of the <see cref="Shared.Button"/> class.
        /// </summary>
        public ColorPicker()
        {
            myUIImageView = new MyUIImageView(this) { };
          
        }
 
        public override void Refresh()
        {
            base.Refresh();
            //Width = (int)myUIImageView.Image.Size.Width;
            //Height = (int)myUIImageView.Image.Size.Height;
        }
 
        string colorImagePath;
        /// <summary>
        /// 色板图
        /// </summary>
        /// <value>The color image path.</value>
        public string ColorImagePath
        {
            get {
                return colorImagePath;
            }
            set
            {
                colorImagePath = value;
 
                myUIImageView.Image = UIImage.FromFile(FileUtils.GetImageFilePath(colorImagePath));
            }
        }
 
        /// <summary>
        /// 颜色变化
        /// </summary>
        public Action<View, byte[]> ColorChaged;
 
        class MyUIImageView : UIImageView
        {
            [Weak] ColorPicker colorPicker;
            public MyUIImageView(ColorPicker colorPicker)
            {
                this.colorPicker = colorPicker;
                UserInteractionEnabled = true;
            }
 
 
            /// <summary>
            /// 点击开始
            /// </summary>
            /// <param name="touches">Touches.</param>
            /// <param name="evt">Evt.</param>
            public override void TouchesBegan(NSSet touches, UIEvent evt)
            {
                colorPicker?.TouchEvent(EventActions.Down,(touches.AnyObject as UITouch).LocationInView(this) );
                colorChaged(colorAtPixel((touches.AnyObject as UITouch).LocationInView(this)));
            }
            /// <summary>
            ///  移动
            /// </summary>
            /// <param name="touches">Touches.</param>
            /// <param name="evt">Evt.</param>
            public override void TouchesMoved(NSSet touches, UIEvent evt)
            {
                colorPicker?.TouchEvent(EventActions.Move,(touches.AnyObject as UITouch).LocationInView(this));
                colorChaged(colorAtPixel((touches.AnyObject as UITouch).LocationInView(this)));
            }
 
            /// <summary>
            /// 点击弹起
            /// </summary>
            /// <param name="touches">Touches.</param>
            /// <param name="evt">Evt.</param>
            public override void TouchesEnded(NSSet touches, UIEvent evt)
            {
                colorPicker?.TouchEvent(EventActions.Up,(touches.AnyObject as UITouch).LocationInView(this));
                colorChaged(colorAtPixel((touches.AnyObject as UITouch).LocationInView(this)));
            }
 
            public override void TouchesCancelled(NSSet touches, UIEvent evt)
            {
                colorPicker?.TouchEvent(EventActions.Cancel, (touches.AnyObject as UITouch).LocationInView(this));
            }
 
         
            void colorChaged(UIColor uiColor)
            {
                nfloat r, g, b, a;
                uiColor.GetRGBA(out r, out g, out b, out a);
                if (r == 0 && g == 0 && b == 0)
                {
                    return;
                }
               colorPicker?.ColorChaged?.Invoke(colorPicker,new byte[] { (byte)(r * 255), (byte)(g * 255), (byte)(b * 255) });
            }
 
            UIColor colorAtPixel(CGPoint point)
            {
                var pointX = (int)(point.X *Image.Size.Width/ Frame.Width);
                var pointY = (int)(point.Y * Image.Size.Height / Frame.Height);
 
                if (Image.Size.Width < pointX) {
                    pointX = (int)Image.Size.Width;
                }
 
                if (Image.Size.Height < pointY) {
                    pointY = (int)Image.Size.Height;
                }
                var cgImage = Image.CGImage;
                var colorSpace = CGColorSpace.CreateDeviceRGB();
                int bytesPerPixel = 4;
                int bytesPerRow = bytesPerPixel * 1;
                var bitsPerComponent = 8;
                byte[] pixelData = { 0, 0, 0, 0 };
                var context = new CGBitmapContext(pixelData,
                                                             1,
                                                             1,
                                                             bitsPerComponent,
                                                             bytesPerRow,
                                                 colorSpace, CGBitmapFlags.PremultipliedLast | CGBitmapFlags.ByteOrder32Big);
 
                context.SetBlendMode(CGBlendMode.Copy);
 
                context.TranslateCTM(-pointX, pointY - Image.Size.Height);
                context.DrawImage(new CGRect(0.0f, 0.0f, Image.Size.Width, Image.Size.Height), cgImage);
                var red = pixelData[0] / 255.0f;
                var green = pixelData[1] / 255.0f;
                var blue = pixelData[2] / 255.0f;
                var alpha = pixelData[3] / 255.0f;
                return UIColor.FromRGBA(red, green, blue, alpha);
            }
        }
 
    }
 
}