WPF绘制圆形调色盘
2024-08-23 05:04:53
本文使用writeableBitmap类和HSB、RGB模式来绘制圆形的调色盘。
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);
}
}
}
最新文章
- Python实现简单的记账本功能
- webpack详细配置讲解
- 友盟页面统计 - 关于Viewpager中的Fragment的生命周期
- NEC学习 ---- 布局 -三列,右侧自适应
- JS运动基础(四) 碰撞运动
- 蓝牙的AVDTP协议笔记
- 我的WCF之旅(3):在WCF中实现双工通信
- oracle当需要commit
- Python练习一
- celery定时任务
- 跨DB Server创建View
- Linux 普通用户免密码切换到root用户
- Netty buffer缓冲区ByteBuf
- Java生成数独函数
- java序列化深拷贝【转】
- Amazon电商数据分析——数据获取
- 【修改帐号信息】Eclipse中修改SVN用户名和密码方法
- ViewPager 如何得到当前的Fragment (使用FragmentPagerAdapter)
- windows本地调试安装hadoop(idea) : ERROR util.Shell: Failed to locate the winutils binary in the hadoop binary path
- tr命令详解
热门文章
- NoSQL,关系型数据库,行列数据库对比、类比
- 题解【CodeForces 910A The Way to Home】
- 6.1 NOI 模拟
- 万答17,AWS RDS怎么搭建本地同步库
- 文件上传接入阿里云OSS
- 听,引擎的声音「GitHub 热点速览 v.22.33」
- 283. 移动零--LeetCode__双指针
- 209. 长度最小的子数组--LeetCode
- OpenCV CMake VSCode Windows 平台下运行配置及其解决方案
- 利用Hugging Face中的模型进行句子相似性实践