作者: RayLinn
最近的项目里需要从索引颜色里取最接近的颜色,用下面的代码实现之:
public static Color ScanNearestColor(Color color)
{
int leastDistance = int.MaxValue;
int red = color.R;
int green = color.G;
int blue = color.B;
byte colorIndex = 255;
Color[] _colors = GetColorPalette();
for (int index = 0; index < _colors.Length; index++)
{
// Lookup the color from the palette
Color paletteColor = _colors[index];
// Compute the distance from our source color to the palette color
int redDistance = paletteColor.R - red;
int greenDistance = paletteColor.G - green;
int blueDistance = paletteColor.B - blue;
int distance = (redDistance * redDistance) +
(greenDistance * greenDistance) +
(blueDistance * blueDistance);
// If the color is closer than any other found so far, use it
if (distance < leastDistance)
{
colorIndex = (byte)index;
leastDistance = distance;
// And if it's an exact match, exit the loop
if (0 == distance)
break;
}
}
return _colors[colorIndex];
}
代码里的GetColorPalette返回索引颜色表,比如下面是生成Web安全色的颜色表:
private static Color[] GetWebSafeColorPalette()
{
Color[] aSafeCols = new Color[216];
int rValue, gValue, bValue;
int iPointer = 0;
for (rValue = 0; rValue <= 255; rValue += 51)
{
for (gValue = 0; gValue <= 255; gValue += 51)
{
for (bValue = 0; bValue <= 255; bValue += 51)
{
aSafeCols[iPointer] = Color.FromRgb((byte)rValue,(byte)gValue,(byte)bValue);
iPointer += 1;
}
}
}
基本最后完成的是个基于Visual studio的CSS编辑器,配上上个项目里为CSS的颜色定义添加相应颜色的下划线,这样的CSS编辑器相当不错了,如果要完美一点,还可以加上图片预览的功能。
参考:
http://msdn.microsoft.com/en-us/library/aa479306.aspx
- 大小: 241.2 KB
分享到:
相关推荐
赠送源代码:simple-xml-safe-2.7.1-sources.jar; 赠送Maven依赖信息文件:simple-xml-safe-2.7.1.pom; 包含翻译后的API文档:simple-xml-safe-2.7.1-javadoc-API文档-中文(简体)版.zip; Maven坐标:...
simple-xml-safe-2.7.1.jar
ANSYS和ANSYS,FE-SAFE软件的工程应用及实例 很有用的资料值得下载看看
json-stringify-safe, 在循环引用( 如 JSON.stringify ) 上,但不引发 json-stringify-safe类似 JSON.stringify,但不引发循环引用。用法采用与 JSON.stringify 相同的参数。var stringify = require('json-...
gson-type-safe-adapter A type safe gson adapter. Introduction This library can help mismatch input types keep correct default value, never null. But keep in mind, input JSON should still be legal ...
Web Safe Color Pallette
kaggle_porto-seguro-safe-driver-prediction.zip
EuroNCAP 2023 V10.0 对DMS/OMS/AEB/LSS进行了约束
奇安信可信浏览器 Deb安装包
赠送源代码:simple-xml-safe-2.7.1-sources.jar; 赠送Maven依赖信息文件:simple-xml-safe-2.7.1.pom; 包含翻译后的API文档:simple-xml-safe-2.7.1-javadoc-API文档-中文(简体)-英语-对照版.zip; Maven坐标:...
资源来自pypi官网。 资源全名:drf-safe-jack-4.0.4.tar.gz
fast-safe-stringify, 安全且快速地序列化JavaScript对象 fast-safe-stringify安全和快速的序列化替代替代 JSON.stringify 。优雅地处理圆形结构而不是抛出。提供确定的("稳定") 版本,也可以优雅地处理圆形结构。 ...
make-promises-safe 一个让promises的使用免于内存泄漏的node.js模块
奇安信浏览器 飞腾 银河麒麟版
前端开源库-max-safe-integermax safe integer,es2015 number.max_safe_integer ponyfill
DeepFM实战的数据集
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
npm install react-native-safe-area-context 然后,您需要为所使用的平台链接库的本机部分。 在React Native中链接> = 0.60 使用不再需要链接软件包。 iOS平台: $ npx pod-install 在React Native <0.60中...
Laravel开发-safe-data-objects 安全数据对象
安全函数-C-safe-library