求颜色算法

qanholas 2010-11-24 12:08:24
给定任意颜色值 如#ffc076
求其颜色浓度为三分之一的颜色值
...全文
141 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
qanholas 2010-11-24
  • 打赏
  • 举报
回复
看不明白,给定一颜色值 蓝色 ,我想要淡蓝的 颜色值
最好给一个方法调用
sunzhi523 2010-11-24
  • 打赏
  • 举报
回复
以后多多交流 哦。。。
兔子-顾问 2010-11-24
  • 打赏
  • 举报
回复
贴到帖子里吧。担心未来某天有人搜索到帖子,。那鬼佬帖子又失效了。多失望。

/* This tool is part of the xRay Toolkit and is provided free of charge by Bob Powell.

* This code is not guaranteed to be free from defects or fit for merchantability in any way.

* By using this tool in your own programs you agree to hold Robert W. Powell free from all

* damages direct or incidental that arise from such use.

* You may use this code free of charge in your own projects on condition that you place the

* following paragraph (enclosed in quotes below) in your applications help or about dialog.

* "Portions of this code provided by Bob Powell. http://www.bobpowell.net"

*/



using System;

using System.Drawing;



namespace xRay.Toolkit.Utilities

{

public class RGBHSL

{



public class HSL

{

public HSL()

{

_h=0;

_s=0;

_l=0;

}



double _h;

double _s;

double _l;



public double H

{

get{return _h;}

set

{

_h=value;

_h=_h>1 ? 1 : _h<0 ? 0 : _h;

}

}



public double S

{

get{return _s;}

set

{

_s=value;

_s=_s>1 ? 1 : _s<0 ? 0 : _s;

}

}



public double L

{

get{return _l;}

set

{

_l=value;

_l=_l>1 ? 1 : _l<0 ? 0 : _l;

}

}

}



public RGBHSL()

{

}



/// <summary>

/// Sets the absolute brightness of a colour

/// </summary>

/// <param name="c">Original colour</param>

/// <param name="brightness">The luminance level to impose</param>

/// <returns>an adjusted colour</returns>

public static Color SetBrightness(Color c, double brightness)

{

HSL hsl = RGB_to_HSL(c);

hsl.L=brightness;

return HSL_to_RGB(hsl);

}



/// <summary>

/// Modifies an existing brightness level

/// </summary>

/// <remarks>

/// To reduce brightness use a number smaller than 1. To increase brightness use a number larger tnan 1

/// </remarks>

/// <param name="c">The original colour</param>

/// <param name="brightness">The luminance delta</param>

/// <returns>An adjusted colour</returns>

public static Color ModifyBrightness(Color c, double brightness)

{

HSL hsl = RGB_to_HSL(c);

hsl.L*=brightness;

return HSL_to_RGB(hsl);

}



/// <summary>

/// Sets the absolute saturation level

/// </summary>

/// <remarks>Accepted values 0-1</remarks>

/// <param name="c">An original colour</param>

/// <param name="Saturation">The saturation value to impose</param>

/// <returns>An adjusted colour</returns>

public static Color SetSaturation(Color c, double Saturation)

{

HSL hsl = RGB_to_HSL(c);

hsl.S=Saturation;

return HSL_to_RGB(hsl);

}



/// <summary>

/// Modifies an existing Saturation level

/// </summary>

/// <remarks>

/// To reduce Saturation use a number smaller than 1. To increase Saturation use a number larger tnan 1

/// </remarks>

/// <param name="c">The original colour</param>

/// <param name="Saturation">The saturation delta</param>

/// <returns>An adjusted colour</returns>

public static Color ModifySaturation(Color c, double Saturation)

{

HSL hsl = RGB_to_HSL(c);

hsl.S*=Saturation;

return HSL_to_RGB(hsl);

}



/// <summary>

/// Sets the absolute Hue level

/// </summary>

/// <remarks>Accepted values 0-1</remarks>

/// <param name="c">An original colour</param>

/// <param name="Hue">The Hue value to impose</param>

/// <returns>An adjusted colour</returns>

public static Color SetHue(Color c, double Hue)

{

HSL hsl = RGB_to_HSL(c);

hsl.H=Hue;

return HSL_to_RGB(hsl);

}



/// <summary>

/// Modifies an existing Hue level

/// </summary>

/// <remarks>

/// To reduce Hue use a number smaller than 1. To increase Hue use a number larger tnan 1

/// </remarks>

/// <param name="c">The original colour</param>

/// <param name="Hue">The Hue delta</param>

/// <returns>An adjusted colour</returns>

public static Color ModifyHue(Color c, double Hue)

{

HSL hsl = RGB_to_HSL(c);

hsl.H*=Hue;

return HSL_to_RGB(hsl);

}



/// <summary>

/// Converts a colour from HSL to RGB

/// </summary>

/// <remarks>Adapted from the algoritm in Foley and Van-Dam</remarks>

/// <param name="hsl">The HSL value</param>

/// <returns>A Color structure containing the equivalent RGB values</returns>

public static Color HSL_to_RGB(HSL hsl)

{

double r=0,g=0,b=0;

double temp1,temp2;



if(hsl.L==0)

{

r=g=b=0;

}

else

{

if(hsl.S==0)

{

r=g=b=hsl.L;

}

else

{

temp2 = ((hsl.L<=0.5) ? hsl.L*(1.0+hsl.S) : hsl.L+hsl.S-(hsl.L*hsl.S));

temp1 = 2.0*hsl.L-temp2;



double[] t3=new double[]{hsl.H+1.0/3.0,hsl.H,hsl.H-1.0/3.0};

double[] clr=new double[]{0,0,0};

for(int i=0;i<3;i++)

{

if(t3[i]<0)

t3[i]+=1.0;

if(t3[i]>1)

t3[i]-=1.0;



if(6.0*t3[i] < 1.0)

clr[i]=temp1+(temp2-temp1)*t3[i]*6.0;

else if(2.0*t3[i] < 1.0)

clr[i]=temp2;

else if(3.0*t3[i] < 2.0)

clr[i]=(temp1+(temp2-temp1)*((2.0/3.0)-t3[i])*6.0);

else

clr[i]=temp1;

}

r=clr[0];

g=clr[1];

b=clr[2];

}

}



return Color.FromArgb((int)(255*r),(int)(255*g),(int)(255*b));



}





//

/// <summary>

/// Converts RGB to HSL

/// </summary>

/// <remarks>Takes advantage of whats already built in to .NET by using the Color.GetHue, Color.GetSaturation and Color.GetBrightness methods</remarks>

/// <param name="c">A Color to convert</param>

/// <returns>An HSL value</returns>

public static HSL RGB_to_HSL (Color c)

{

HSL hsl = new HSL();



hsl.H=c.GetHue()/360.0; // we store hue as 0-1 as opposed to 0-360

hsl.L=c.GetBrightness();

hsl.S=c.GetSaturation();



return hsl;

}

}

}
bu88dong 2010-11-24
  • 打赏
  • 举报
回复
颜色还可以这样哦、学习下。
兔子-顾问 2010-11-24
  • 打赏
  • 举报
回复
连接有点问题,google翻译的
原文
http://www.bobpowell.net/rgbhsb.htm
兔子-顾问 2010-11-24
  • 打赏
  • 举报
回复
浓度…

饱和度

搜索一下c# RGB HSB 转换
例如
http://soaspx.com/dotnet/csharp/csharp_20101014_5963.html
qanholas 2010-11-24
  • 打赏
  • 举报
回复
搞定了

Color C = Color.Blue;
button1.BackColor = RGBHSL.SetBrightness(C, 0.8);

这个 0.8就是想要的效果 谢谢wuyazhe!

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧