Rgb和Hsv相互转换的方法!


  以下是我在进行Rgb和Hsv色彩模式相互转换的方法,经过两个软件使用和测试!挖坑笔记!

  

None.gif using  System;
None.gif
using  System.Drawing;
None.gif
None.gif
namespace  EngTyping.Class
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// ColorHandle 处理Rgb|Hsv的相互转换
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    internal class ColorHandle
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public int H,S,V;
InBlock.gif        
public int R,G,B;
InBlock.gif        
public int C,M,Y,K;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 处理Rgb到Hsv的转换
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="R"></param>
InBlock.gif        
/// <param name="G"></param>
InBlock.gif        
/// <param name="B"></param>
ExpandedSubBlockEnd.gif        
/// 

InBlock.gif        public void RgbToHsv(int R,int G,int B)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Color MyColor
=Color.FromArgb(R,G,B);
InBlock.gif            H
=Convert.ToInt32(MyColor.GetHue());
InBlock.gif
InBlock.gif            
//奇怪——用微软自己的方法获得的S值和V值居然不对
InBlock.gif            
//S=Convert.ToInt32(MyColor.GetSaturation()/255*100);
InBlock.gif            
//V=Convert.ToInt32(MyColor.GetBrightness()/255*100);
InBlock.gif

InBlock.gif            
decimal min;
InBlock.gif            
decimal max;
InBlock.gif            
decimal delta;
InBlock.gif
InBlock.gif            
decimal R1=Convert.ToDecimal(R)/255;
InBlock.gif            
decimal G1=Convert.ToDecimal(G)/255;
InBlock.gif            
decimal B1=Convert.ToDecimal(B)/255;
InBlock.gif
InBlock.gif            min 
= Math.Min(Math.Min(R1, G1), B1);
InBlock.gif            max 
= Math.Max(Math.Max(R1, G1), B1);
InBlock.gif            V 
= Convert.ToInt32(max*100);
InBlock.gif            delta 
= (max - min)*100;
InBlock.gif
InBlock.gif            
if (max == 0 || delta == 0)
InBlock.gif                S 
= 0;
InBlock.gif            
else
InBlock.gif                S 
= Convert.ToInt32(delta / max);
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
public void RgbToHsv(Color MyColor)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//Color MyColor=Color.FromArgb(R,G,B);
InBlock.gif
            int R = MyColor.R;
InBlock.gif            
int G = MyColor.G;
InBlock.gif            
int B = MyColor.B;
InBlock.gif            H
=Convert.ToInt32(MyColor.GetHue());
InBlock.gif
InBlock.gif            
//奇怪——用微软自己的方法获得的S值和V值居然不对
InBlock.gif            
//S=Convert.ToInt32(MyColor.GetSaturation()/255*100);
InBlock.gif            
//V=Convert.ToInt32(MyColor.GetBrightness()/255*100);
InBlock.gif

InBlock.gif            
decimal min;
InBlock.gif            
decimal max;
InBlock.gif            
decimal delta;
InBlock.gif
InBlock.gif            
decimal R1=Convert.ToDecimal(R)/255;
InBlock.gif            
decimal G1=Convert.ToDecimal(G)/255;
InBlock.gif            
decimal B1=Convert.ToDecimal(B)/255;
InBlock.gif
InBlock.gif            min 
= Math.Min(Math.Min(R1, G1), B1);
InBlock.gif            max 
= Math.Max(Math.Max(R1, G1), B1);
InBlock.gif            V 
= Convert.ToInt32(max*100);
InBlock.gif            delta 
= (max - min)*100;
InBlock.gif
InBlock.gif            
if (max == 0 || delta == 0)
InBlock.gif                S 
= 0;
InBlock.gif            
else
InBlock.gif                S 
= Convert.ToInt32(delta / max);
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 处理Hsv到Rgb的转换
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="H"></param>
InBlock.gif        
/// <param name="S"></param>
ExpandedSubBlockEnd.gif        
/// <param name="V"></param>

InBlock.gif        public void HsvToRgb(int H,int S,int V)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            H
=Convert.ToInt32(Convert.ToDecimal( H )/360*255);
InBlock.gif            S
=Convert.ToInt32(Convert.ToDecimal( S )/100*255);
InBlock.gif            V
=Convert.ToInt32(Convert.ToDecimal( V )/100*255);
InBlock.gif
InBlock.gif            
if (S==0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                R
=0;
InBlock.gif                G
=0;
InBlock.gif                B
=0;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
decimal fractionalSector;
InBlock.gif            
decimal sectorNumber;
InBlock.gif            
decimal sectorPos;
InBlock.gif            sectorPos 
= (Convert.ToDecimal(H)/255*360)/60;
InBlock.gif            sectorNumber 
= Convert.ToInt32(Math.Floor(Convert.ToDouble(sectorPos)));
InBlock.gif            fractionalSector 
= sectorPos - sectorNumber;
InBlock.gif
InBlock.gif            
decimal p;
InBlock.gif            
decimal q;
InBlock.gif            
decimal t;
InBlock.gif
InBlock.gif            
decimal r=0;
InBlock.gif            
decimal g=0;
InBlock.gif            
decimal b=0;
InBlock.gif            
decimal ss=Convert.ToDecimal(S)/255;
InBlock.gif            
decimal vv=Convert.ToDecimal(V)/255;
InBlock.gif            
InBlock.gif            
InBlock.gif            p 
=vv * (1 - ss);
InBlock.gif            q 
= vv * (1 - (ss * fractionalSector));
InBlock.gif            t 
= vv * (1 - (ss * (1 - fractionalSector)));
InBlock.gif          
InBlock.gif            
switch (Convert.ToInt32(sectorNumber))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
case 0:
InBlock.gif                
InBlock.gif                    r 
=vv;
InBlock.gif                    g 
= t;
InBlock.gif                    b 
= p;
InBlock.gif                    
break;
InBlock.gif            
InBlock.gif                
case 1:
InBlock.gif                    r 
= q;
InBlock.gif                    g 
= vv;
InBlock.gif                    b 
= p;
InBlock.gif                    
break;
InBlock.gif                
case 2:
InBlock.gif                
InBlock.gif                    r 
= p;
InBlock.gif                    g 
= vv;
InBlock.gif                    b 
= t;
InBlock.gif                    
break;
InBlock.gif                
case 3:
InBlock.gif                
InBlock.gif                    r 
= p;
InBlock.gif                    g 
= q;
InBlock.gif                    b 
= vv;
InBlock.gif                    
break;
InBlock.gif                
case 4:
InBlock.gif                
InBlock.gif                    r 
= t;
InBlock.gif                    g 
= p;
InBlock.gif                    b 
= vv;
InBlock.gif                    
break;
InBlock.gif                
case 5:
InBlock.gif                
InBlock.gif                    r 
= vv;
InBlock.gif                    g 
= p;
InBlock.gif                    b 
= q;
InBlock.gif                    
break;    
ExpandedSubBlockEnd.gif            }

InBlock.gif            R
= Convert.ToInt32(r * 255);  
InBlock.gif            G
= Convert.ToInt32(g * 255);  
InBlock.gif            B
= Convert.ToInt32(b * 255);  
InBlock.gif     
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// RgbtoCmy
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="R"></param>
InBlock.gif        
/// <param name="G"></param>
ExpandedSubBlockEnd.gif        
/// <param name="B"></param>

InBlock.gif        public void RgbToCmyk(decimal R,decimal G,decimal B)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            C 
= Convert.ToInt32((255 - Math.Abs(R)) * 100 / 255);
InBlock.gif       
InBlock.gif            M 
= Convert.ToInt32((255 - Math.Abs(G)) * 100 / 255);
InBlock.gif      
InBlock.gif            Y 
= Convert.ToInt32((255 - Math.Abs(B)) * 100 / 255);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif

转载于:https://www.cnblogs.com/amwson/archive/2007/04/25/726931.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值