本文使用writeableBitmap类和HSB、RGB模式来绘制圆形的调色盘。

开源项目地址:https://github.com/ZhiminWei/Palette

RGB为可见光波段三个颜色通道,灰度值范围为0-255,HSB模式,H是色相:取值范围0-360°,S是饱和度:取值范围0-100%,B是亮度:取值范围是0-100%,本文仅展示了部分代码,详细代码见上述项目地址。

程序截图:

中国传统色示例:


渲染圆形调试盘主要代码
		private void RenderColorPicker(double brightness)
{
bitmap = new WriteableBitmap(radius * 2 + 20, radius * 2 + 20, 96.0, 96.0, PixelFormats.Pbgra32, null);
Utility.DrawingAllPixel(bitmap, (x, y) =>
{
RGBColor rgb = new RGBColor(255, 255, 255, 0);
double H = 0;
Vector vector = Point.Subtract(new Point(x, y), new Point(radius + 10, radius + 10));
var angle = Math.Atan(vector.Y / vector.X) * 180 / Math.PI;
if (vector.X < 0)
{
H = 270 + angle;
}
else if (vector.X > 0)
{
H = 90 + angle;
}
else
{
if (vector.Y < 0)
{
H = 0;
}
else if (vector.Y > 0)
{
H = 180;
}
else
{
return new RGBColor(255, (int)(255 * brightness), (int)(255 * brightness), (int)(255 * brightness));
}
}
//计算饱和度
double S;
if (vector.Length >= radius)
{
S = 1;
}
else
{
S = vector.Length / radius;
}
//亮度值
double B = brightness;
return new HSBColor(H, S, B).RgbColor;
});
this.img.Source = bitmap;
} /// <summary>
/// 绘制所有像素
/// </summary>
/// <param name="bitmap"></param>
/// <param name="action"></param>
public static void DrawingAllPixel(WriteableBitmap bitmap, Func<int, int, RGBColor> func)
{
//跨距 :针对跨距(stride)的计算,WritePixels()方法需要跨距。
//从技术角度看,跨距是每行像素数据需要的字节数量。
//可通过将每行中像素的数量乘上所使用格式的每像素位数(通常为4,如本例使用的Bgra32格式)
//然后将所得结果除以8,进而将其从位数转换成字节数。
int stride = bitmap.PixelWidth * bitmap.Format.BitsPerPixel / 8; for (int y = 0; y < bitmap.PixelHeight; y++)
{
for (int x = 0; x < bitmap.PixelWidth; x++)
{
var rgb = func.Invoke(x, y);
byte[] colorData = new byte[4] { (byte)rgb.B, (byte)rgb.G, (byte)rgb.R, (byte)rgb.A }; bitmap.WritePixels(new Int32Rect(x, y, 1, 1), colorData, stride, 0);
}
}
}

最新文章

  1. Python实现简单的记账本功能
  2. webpack详细配置讲解
  3. 友盟页面统计 - 关于Viewpager中的Fragment的生命周期
  4. NEC学习 ---- 布局 -三列,右侧自适应
  5. JS运动基础(四) 碰撞运动
  6. 蓝牙的AVDTP协议笔记
  7. 我的WCF之旅(3):在WCF中实现双工通信
  8. oracle当需要commit
  9. Python练习一
  10. celery定时任务
  11. 跨DB Server创建View
  12. Linux 普通用户免密码切换到root用户
  13. Netty buffer缓冲区ByteBuf
  14. Java生成数独函数
  15. java序列化深拷贝【转】
  16. Amazon电商数据分析——数据获取
  17. 【修改帐号信息】Eclipse中修改SVN用户名和密码方法
  18. ViewPager 如何得到当前的Fragment (使用FragmentPagerAdapter)
  19. windows本地调试安装hadoop(idea) : ERROR util.Shell: Failed to locate the winutils binary in the hadoop binary path
  20. tr命令详解

热门文章

  1. NoSQL,关系型数据库,行列数据库对比、类比
  2. 题解【CodeForces 910A The Way to Home】
  3. 6.1 NOI 模拟
  4. 万答17,AWS RDS怎么搭建本地同步库
  5. 文件上传接入阿里云OSS
  6. 听,引擎的声音「GitHub 热点速览 v.22.33」
  7. 283. 移动零--LeetCode__双指针
  8. 209. 长度最小的子数组--LeetCode
  9. OpenCV CMake VSCode Windows 平台下运行配置及其解决方案
  10. 利用Hugging Face中的模型进行句子相似性实践