private void UpdateRgbFromHsv() { Hsv hsv = new Hsv() { H = this.hue, S = this.saturation / 100.0, V = this.value / 100.0 }; Rgb rgb = hsv.To <Rgb>(); if (this.red != (int)rgb.R) { this.red = (int)rgb.R; this.OnPropertyChanged("Red"); } if (this.green != (int)rgb.G) { this.green = (int)rgb.G; this.OnPropertyChanged("Green"); } if (this.blue != (int)rgb.B) { this.blue = (int)rgb.B; this.OnPropertyChanged("Blue"); } }
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { Color color = _lastColor; Hsv colhsv = _lastHsv; var intensity = (double)value; Rgb col = new Rgb { R = 0, B = 0, G = 0 }; col.R = color.R; col.G = color.G; col.B = color.B; switch ((string)parameter) { case "r": colhsv.H = intensity; //color.R = intensity; break; case "g": colhsv.S = intensity; //color.G = intensity; break; case "b": colhsv.V = intensity; //color.B = intensity; break; case "a": //color.A = intensity; break; } col = colhsv.To <Rgb>(); color.R = (byte)col.R; color.G = (byte)col.G; color.B = (byte)col.B; _lastColor = color; _lastHsv = colhsv; return(color); }
} //Sonogram2Image() public static Image CreateFalseColourDecibelSpectrogram(double[,] dbSpectrogramData, double[,] nrSpectrogramData, byte[,] hits) { double truncateMin = -120.0; double truncateMax = -30.0; double filterCoefficient = 1.0; double[,] dbSpectrogramNorm = NormaliseSpectrogramMatrix(dbSpectrogramData, truncateMin, truncateMax, filterCoefficient); truncateMin = 0; truncateMax = 60; double[,] nrSpectrogramNorm = NormaliseSpectrogramMatrix(nrSpectrogramData, truncateMin, truncateMax, filterCoefficient); int width = dbSpectrogramData.GetLength(0); int height = dbSpectrogramData.GetLength(1); Bitmap image = new Bitmap(width, height); Color[] ridgeColours = { Color.Red, Color.DarkMagenta, Color.Black, Color.LightPink }; for (int y = 0; y < height; y++) //over all freq bins { for (int x = 0; x < width; x++) //for pixels in the line { // NormaliseMatrixValues and bound the value - use min bound, max and 255 image intensity range double dbValue = dbSpectrogramNorm[x, y]; int c1 = 255 - (int)Math.Floor(255.0 * dbValue); //original version //int c1 = (int)Math.Floor(255.0 * dbValue); if (c1 < 0) { c1 = 0; } else if (c1 > 255) { c1 = 255; } var colour = Color.FromArgb(c1, c1, c1); if (nrSpectrogramNorm[x, y] > 0) { // use HSV colour space int bottomColour = 30; // to avoid using the reds int topColour = 320; // to avoid using the magentas int hueRange = topColour - bottomColour; int hue = bottomColour + (int)Math.Floor(hueRange * nrSpectrogramNorm[x, y]); double saturation = 1.0; //double saturation = 0.75 + (nrSpectrogramNorm[x, y] * 0.25); //double saturation = nrSpectrogramNorm[x, y] * 0.5; //double saturation = (1 - nrSpectrogramNorm[x, y]) * 0.5; double value = 1.0; //double value = 0.60 + (nrSpectrogramNorm[x, y] * 0.40); var myHsv = new Hsv { H = hue, S = saturation, V = value }; var myRgb = myHsv.To <Rgb>(); colour = Color.FromArgb((int)myRgb.R, (int)myRgb.G, (int)myRgb.B); // get colour for noise reduced portion // superimpose ridge detection if (hits[x, y] > 0) { //value = 0.60 + (nrSpectrogramNorm[x, y] * 0.40); //myHsv = new Hsv { H = 260, S = saturation, V = value }; //myRgb = myHsv.To<Rgb>(); //colour = Color.FromArgb((int)myRgb.R, (int)myRgb.G, (int)myRgb.B); colour = ridgeColours[hits[x, y] - 1]; } } image.SetPixel(x, height - y - 1, colour); } }//end over all freq bins //image.Save(@"C:\SensorNetworks\Output\Sonograms\TEST3.png", ImageFormat.Png); return(image); }
static void Main(string[] args) { var list = DeviceList.Local; HidDevice XD75 = null; HidStream XD75Strm = null; bool reportEnabled = false; keyCounter = Extensions.ReadCounter(); XD75 = TryConnectXD75(); DeviceList.Local.Changed += (object sender, DeviceListChangedEventArgs a) => { if (XD75 == null) { XD75 = TryConnectXD75(); } }; Thread heartBeatThread = new Thread(() => { while (true) { Thread.Sleep(5000); if (XD75 != null && XD75Strm != null) { var heartBeatMessage = Enums.RAW_COMMAND_ID.RAW_COMMAND_HEARTBEAT_PING.ConstructRawCommand(); lock (StreamLock) { XD75Strm.Write(heartBeatMessage); } } } }); Thread mainThread = new Thread(() => { while (true) { if (XD75 == null) { Console.WriteLine("No XD75 found, searching in background..."); while (XD75 == null) { Thread.Sleep(100); } } if (XD75.TryOpen(out XD75Strm)) { reportEnabled = EnableReport(XD75Strm); if (reportEnabled) { XD75Strm.ReadTimeout = Timeout.Infinite; Console.CancelKeyPress += delegate { var DisableKeyEventReportCmd = Enums.RAW_COMMAND_ID.RAW_COMMAND_DISABLE_KEY_EVENT_REPORT.ConstructRawCommand(); lock (StreamLock) { XD75Strm?.Write(DisableKeyEventReportCmd); XD75Strm.ReadTimeout = 50; XD75Strm?.Close(); } keyCounter.DumpCounter(); XD75 = null; XD75Strm = null; heartBeatThread.Abort(); System.Environment.Exit(0); }; while (true) { if (!heartBeatThread.IsAlive) { heartBeatThread.Start(); } try { var report = XD75Strm.Read(); if (report[1] == Enums.RAW_COMMAND_ID.RAW_COMMAND_HEARTBEAT_PING.ToByte()) { Console.WriteLine("Received heartbeat response."); var heartBeatMsg = Enums.RAW_COMMAND_ID.RAW_COMMAND_HEARTBEAT_PING.ConstructRawCommand(); var response = report.Take(heartBeatMsg.Count()).ToArray(); if (!response.SequenceEqual(heartBeatMsg)) { Console.WriteLine($"Heart beat message of no.{HeartBeatCount} mismatches..."); } else { Console.WriteLine($"Heart beat message of no.{HeartBeatCount} matches."); HeartBeatCount++; continue; } } if (report[1] == Enums.RAW_COMMAND_ID.RAW_COMMAND_CHANGE_COLOR.ToByte()) { Console.WriteLine("Received change underglow color response."); if (report[2] == 1) { if (report[3] == FAILED) { Console.WriteLine("Command failed."); } else { Console.WriteLine("Command succeded unexpectely."); } } else if (report[2] == 4) { if (report[6] == SUCCESS) { Hsv hsv = new Hsv(); hsv.H = report[3] * 360d / 255d; hsv.S = report[4] / 255d; hsv.V = report[5] / 255d; Rgb rgb = hsv.To <Rgb>(); Console.WriteLine($"Command succeded with value:{rgb.R} {rgb.G} {rgb.B}"); } else { Console.WriteLine($"Command failed unexpectely."); } } continue; } if (report[1] != Enums.RAW_COMMAND_ID.RAW_COMMAND_REPORT_KEY_EVENT.ToByte()) { Console.WriteLine("Received unknown command:"); Console.WriteLine(BitConverter.ToString(report)); continue; } int len = report[2]; if (len != 0x2) { Console.WriteLine("Received unknown report:"); Console.WriteLine(BitConverter.ToString(report)); continue; } int col = (int)report[3]; //0-14 int row = (int)report[4]; //0-4 keyCounter[row * 15 + col]++; Console.WriteLine($"Received key event: {col},{row}"); Console.WriteLine($"Total KeyCount: {keyCounter.Sum()}"); if (keyCounter.Sum() % 100 == 0) { Console.WriteLine("Saving keyCounter..."); keyCounter.DumpCounter(); } } catch (Exception e) { XD75Strm?.Close(); reportEnabled = false; XD75 = null; XD75Strm = null; break; } } continue; } else { Console.WriteLine("Report enable failed."); } } else { Console.WriteLine("Error communicating with XD75."); continue; } XD75Strm?.Close(); } }); mainThread.Start(); while (true) { var inputs = Console.ReadLine()?.Split(); if (inputs == null) { break; } if (inputs[0].ToLower() == "c") { if (inputs.Length == 4) { try { var payload = inputs.Skip(1).Select(s => { if (s[0] == 'd') { return(Convert.ToByte(s.Trim('d'), 10)); } return(Convert.ToByte(s, 16)); }).ToArray(); Rgb rgb = new Rgb(); rgb.R = payload[0]; rgb.G = payload[1]; rgb.B = payload[2]; Console.WriteLine("RGB: " + payload[0].ToString("X2") + payload[1].ToString("X2") + payload[2].ToString("X2")); var hsv = rgb.To <Hsv>(); payload[0] = (byte)(hsv.H / 360d * 255d); payload[1] = (byte)(hsv.S * 255d); payload[2] = (byte)(hsv.V * 255d); Console.WriteLine(payload[0].ToString() + " " + payload[1].ToString() + " " + payload[2].ToString()); if (XD75 != null && XD75Strm != null) { var colorMessage = Enums.RAW_COMMAND_ID.RAW_COMMAND_CHANGE_COLOR.ConstructRawCommand(payload); lock (StreamLock) { XD75Strm.Write(colorMessage); } } } catch { }; } } } }