国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

#673 – 將鼠標位置和顏色進行映射(Mapping Mouse Position to Color)

2019-11-06 06:13:24
字體:
來源:轉載
供稿:網友

我們可以將鼠標的位置信息隱射成顏色的色調和飽和度,然后將HSV顏色值轉換為RGB顏色值(我們將亮度值設為1.0)。我們將鼠標位置和中心點連線的角度作為色調值,將鼠標和中心點的距離作為飽和度值。這個圖像模型如下圖:

下面的代碼中我們有一個400*400的Canvas,取鼠標在其上面的位置算出一個顏色顯示在Label中。

<Window Name="win1" x:Class="Wpfapplication1.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/PResentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="Color from Mouse Position"        SizeToContent="WidthAndHeight"        MouseMove="win1_MouseMove_1">     <Canvas x:Name="canv1" Width="400" Height="400">        <Label Content="{Binding RGBInfo}" HorizontalAlignment="Center" />    </Canvas></Window>

在后臺代碼中,我們注冊MouseMove 事件,并在里面根據鼠標位置計算顏色,作為背景色顯示。

/// <summary>////// Interaction logic for MainWindow.xaml/// </summary>public partial class MainWindow : Window, INotifyPropertyChanged{    SolidColorBrush backBrush = new SolidColorBrush();     public MainWindow()    {        InitializeComponent();         this.DataContext = this;        win1.Background = backBrush;    }     private string rgbInfo;    public string RGBInfo  // 用于顯示顏色信息的字符串    {        get { return rgbInfo; }        set        {            if (value != rgbInfo)            {                rgbInfo = value;                RaisePropertyChanged("RGBInfo");            }        }    }     private void win1_MouseMove_1(object sender, MouseEventArgs e)  // 鼠標移動事件,計算顏色    {        double radius = (canv1.ActualWidth / 2);         Color c = ColorFromMousePosition(e.GetPosition(canv1), radius);        backBrush.Color = c;        RGBInfo = string.Format("R={0}, G={1}, B={2}", c.R, c.G, c.B);    }     Color ColorFromMousePosition(Point mousePos, double radius)    {        // 相對canvas中心點的坐標位置        double xRel = mousePos.X - radius;        double yRel = mousePos.Y - radius;         // 計算色調, 0-360        double angleRadians = Math.Atan2(yRel, xRel);        double hue = angleRadians * (180 / Math.PI);        if (hue < 0)            hue = 360 + hue;         // 飽和度是鼠標到中心點的距離和半徑的比值,0-1        double saturation = Math.Min(Math.Sqrt(xRel * xRel + yRel * yRel) / radius, 1.0);         byte r, g, b;        ColorUtil.HsvToRgb(hue, saturation, 1.0, out r, out g, out b);        return Color.FromRgb(r, g, b);    }     public event PropertyChangedEventHandler PropertyChanged;     private void RaisePropertyChanged(string prop)    {        if (PropertyChanged != null)            PropertyChanged(this, new PropertyChangedEventArgs(prop));    }}

下面是HSV轉換RGB的代碼http://www.splinter.com.au/converting-hsv-to-rgb-colour-using-c/

public static class ColorUtil{    /// <summary>    /// Convert HSV to RGB    /// h is from 0-360    /// s,v values are 0-1    /// r,g,b values are 0-255    /// Based upon http://ilab.usc.edu/wiki/index.php/HSV_And_H2SV_Color_Space#HSV_Transformation_C_.2F_C.2B.2B_Code_2    /// </summary>    public static void HsvToRgb(double h, double S, double V, out byte r, out byte g, out byte b)    {        // ######################################################################        // T. Nathan Mundhenk        // mundhenk@usc.edu        // C/C++ Macro HSV to RGB         double H = h;        while (H < 0) { H += 360; };        while (H >= 360) { H -= 360; };        double R, G, B;        if (V <= 0)        { R = G = B = 0; }        else if (S <= 0)        {            R = G = B = V;        }        else        {            double hf = H / 60.0;            int i = (int)Math.Floor(hf);            double f = hf - i;            double pv = V * (1 - S);            double qv = V * (1 - S * f);            double tv = V * (1 - S * (1 - f));            switch (i)            {                 // Red is the dominant color                 case 0:                    R = V;                    G = tv;                    B = pv;                    break;                 // Green is the dominant color                 case 1:                    R = qv;                    G = V;                    B = pv;                    break;                case 2:                    R = pv;                    G = V;                    B = tv;                    break;                 // Blue is the dominant color                 case 3:                    R = pv;                    G = qv;                    B = V;                    break;                case 4:                    R = tv;                    G = pv;                    B = V;                    break;                 // Red is the dominant color                 case 5:                    R = V;                    G = pv;                    B = qv;                    break;                 // Just in case we overshoot on our math by a little, we put these here. Since its a switch it won't slow us down at all to put these here.                 case 6:                    R = V;                    G = tv;                    B = pv;                    break;                case -1:                    R = V;                    G = pv;                    B = qv;                    break;                 // The color is not defined, we should throw an error.                 default:                    //LFATAL("i Value error in Pixel conversion, Value is %d", i);                    R = G = B = V; // Just pretend its black/white                    break;            }        }        r = Clamp((byte)(R * 255.0));        g = Clamp((byte)(G * 255.0));        b = Clamp((byte)(B * 255.0));    }     /// <summary>    /// Clamp a value to 0-255    /// </summary>    private static byte Clamp(byte i)    {        if (i < 0) return 0;        if (i > 255) return 255;        return i;    }}

可以看到,隨著鼠標的移動,窗口的背景色也不斷的變化。

原文地址:https://wpf.2000things.com/2012/10/22/673-mapping-mouse-position-to-color/

******************************************************譯者注*******************************************************HSV分別表示的是色調、飽和度和亮度。上面的例子中改變的是色調和飽和度,亮度通常取值為0到1的值。我們可以通過其他方式來映射改變亮度是值,比如鼠標通過滾輪增加和減少亮度。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 双桥区| 昌吉市| 台湾省| 个旧市| 蚌埠市| 阿荣旗| 会东县| 万山特区| 海安县| 北流市| 莆田市| 边坝县| 梁山县| 德钦县| 昌黎县| 梨树县| 德江县| 犍为县| 德庆县| 和静县| 郴州市| 高邮市| 峨眉山市| 偏关县| 隆回县| 扎兰屯市| 陇西县| 乌拉特前旗| 陆川县| 邛崃市| 棋牌| 昔阳县| 温州市| 德兴市| 惠州市| 江西省| 维西| 烟台市| 鹤庆县| 图们市| 思南县|