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 event EventHandler<byte[]> ColorChaged;
|
|
class MyUIImageView : UIImageView
|
{
|
View _view;
|
public MyUIImageView(View view)
|
{
|
_view = view;
|
UserInteractionEnabled = true;
|
}
|
|
|
|
/// <summary>
|
/// 点击开始
|
/// </summary>
|
/// <param name="touches">Touches.</param>
|
/// <param name="evt">Evt.</param>
|
public override void TouchesBegan(NSSet touches, UIEvent evt)
|
{
|
//base.TouchesBegan(touches, evt);
|
UITouch touch = touches.AnyObject as UITouch;
|
_view.TouchEvent(EventActions.Down, touch.LocationInView(this));
|
colorChaged(colorAtPixel(touch.LocationInView(this)));
|
}
|
/// <summary>
|
/// 移动
|
/// </summary>
|
/// <param name="touches">Touches.</param>
|
/// <param name="evt">Evt.</param>
|
public override void TouchesMoved(NSSet touches, UIEvent evt)
|
{
|
//base.TouchesMoved(touches, evt);
|
UITouch touch = touches.AnyObject as UITouch;
|
_view.TouchEvent(EventActions.Move, touch.LocationInView(this));
|
colorChaged(colorAtPixel(touch.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;
|
}
|
if (_view != null && ((ColorPicker)_view).ColorChaged != null)
|
{
|
(_view as ColorPicker).ColorChaged(_view, new byte[] {(byte)(r * 255),(byte)(g * 255),(byte)(b*255)});
|
}
|
}
|
/// <summary>
|
/// 点击弹起
|
/// </summary>
|
/// <param name="touches">Touches.</param>
|
/// <param name="evt">Evt.</param>
|
public override void TouchesEnded(NSSet touches, UIEvent evt)
|
{
|
//base.TouchesEnded(touches, evt);
|
UITouch touch = touches.AnyObject as UITouch;
|
_view.TouchEvent(EventActions.Up, touch.LocationInView(this));
|
colorChaged(colorAtPixel(touch.LocationInView(this)));
|
}
|
|
|
UIColor colorAtPixel(CGPoint point)
|
{
|
var pointX = (int)(point.X *Image.Size.Width/ Frame.Width);
|
var pointY = (int)(point.Y * Image.Size.Height / Frame.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);
|
}
|
}
|
|
}
|
|
}
|