public RGB Convert(HSI input) { var h = input.H.Modulo(0, 360) * Math.PI / 180.0; var s = input.S.Coerce(100) / 100.0; var i = input.I.Coerce(255) / 255.0; var pi3 = Math.PI / 3; double r, g, b; if (h < (2 * pi3)) { b = i * (1 - s); r = i * (1 + (s * Math.Cos(h) / Math.Cos(pi3 - h))); g = i * (1 + (s * (1 - Math.Cos(h) / Math.Cos(pi3 - h)))); } else if (h < (4 * pi3)) { h = h - 2 * pi3; r = i * (1 - s); g = i * (1 + (s * Math.Cos(h) / Math.Cos(pi3 - h))); b = i * (1 + (s * (1 - Math.Cos(h) / Math.Cos(pi3 - h)))); } else { h = h - 4 * pi3; g = i * (1 - s); b = i * (1 + (s * Math.Cos(h) / Math.Cos(pi3 - h))); r = i * (1 + (s * (1 - Math.Cos(h) / Math.Cos(pi3 - h)))); } return(new RGB(new Vector(r, g, b).Coerce(0, 1))); }
/// <summary> /// Convert HSI colorspace to RGB colorspace /// </summary> /// <param name="hsi">Input HSI pixel</param> /// <returns>RGB colorspace pixel</returns> public static RGB HSI2RGB(HSI hsi) { double r, g, b; double h = hsi.Hue; double s = hsi.Saturation; double i = hsi.Intensity; h = h * 2 * Math.PI; if (h >= 0 && h < 2 * Math.PI / 3) { b = i * (1 - s); r = i * (1 + s * Math.Cos(h) / Math.Cos(Math.PI / 3 - h)); g = 3 * i - (r + b); } else if (h >= 2 * Math.PI / 3 && h < 4 * Math.PI / 3) { r = i * (1 - s); g = i * (1 + s * Math.Cos(h - 2 * Math.PI / 3) / Math.Cos(Math.PI - h)); b = 3 * i - (r + g); } else //if (h >= 4 * Math.PI / 3 && h <= 2 * Math.PI) { g = i * (1 - s); b = i * (1 + s * Math.Cos(h - 4 * Math.PI / 3) / Math.Cos(5 * Math.PI / 3 - h)); r = 3 * i - (g + b); } return(new RGB((byte)(r * 255.0 + .5), (byte)(g * 255.0 + .5), (byte)(b * 255.0 + .5))); }
private void btntran_Click(object sender, EventArgs e) { Bitmap source = (Bitmap)pictureBox1.Image; int[] graylevel = new int[256]; HSI[,] hsi = new HSI[source.Height, source.Width]; for (int i = 0; i < source.Height; i++) { for (int j = 0; j < source.Width; j++) { RGB rgb = new RGB(source.GetPixel(j, i).R, source.GetPixel(j, i).G, source.GetPixel(j, i).B); hsi[i, j] = to_HSI(rgb); } } //// for (int i = 0; i < source.Height; i++) { for (int j = 0; j < source.Width; j++) { int inx = Convert.ToInt32(hsi[i, j].Intensity * 255.0); graylevel[inx]++; } } double[] Transform = new double[256]; for (int i = 0; i < 256; i++) { Transform[i] = (double)graylevel[i] / (double)(source.Height * source.Width); } for (int i = 1; i < 256; i++) { Transform[i] = Transform[i] + Transform[i - 1]; } for (int i = 0; i < source.Height; i++) { for (int j = 0; j < source.Width; j++) { int inx = Convert.ToInt32(hsi[i, j].Intensity * 255.0); hsi[i, j].Intensity = Transform[inx]; } } //// Bitmap result = new Bitmap(source.Width, source.Height); for (int i = 0; i < source.Height; i++) { for (int j = 0; j < source.Width; j++) { RGB rgb = new RGB(); rgb = to_RGB(hsi[i, j]); result.SetPixel(j, i, Color.FromArgb(rgb.Red, rgb.Green, rgb.Blue)); } } pictureBox2.Image = result; }
public HttpServer(string host, int port) { Host = host; Port = port; Commands = new Dictionary <string, Func <string, string> >(); RegisterCommand("Hi hyjican, tell me the color please. Thank you!", (data) => { var pos = Control.MousePosition; var color = ColorUtil.GetColor(pos); var hsl = new HSL(color.GetHue(), color.GetSaturation(), color.GetBrightness()); var hsb = HSB.Parse(color); var hsi = HSI.Parse(color, Settings.Main.HsiAlgorithm); return(string.Format("It's my pleasure, the following lines shows the color at pixel ({0},{1}) in different formats.\n", pos.X, pos.Y) + color.ToArgb() + "\n" + string.Format("#{0:X2}{1:X2}{2:X2}\nRGB({0},{1},{2})\n", color.R, color.G, color.B) + string.Format("HSL({0},{1},{2})\n", Math.Round(hsl.H), Util.Round(hsl.S * 100), Util.Round(hsl.L * 100)) + string.Format("HSB({0},{1},{2})\n", Math.Round(hsb.H), Util.Round(hsb.S * 100), Util.Round(hsb.B * 100)) + string.Format("HSI({0},{1},{2})", Math.Round(hsi.H), Util.Round(hsi.S * 100), Util.Round(hsi.I * 100))); }); RegisterCommand("Hi hyjican, I would like to take a screenshot.", (data) => { var rect = Util.GetCurrentScreen(); var img = ScreenShot.GetScreen(rect.X, rect.Y, rect.Width, rect.Height); using (var stream = new MemoryStream()) { img.Save(stream, ImageFormat.Jpeg); return(string.Format("OK, the next line is the image data in encoding base64.\n{0}", Convert.ToBase64String(stream.ToArray()))); } }); }
public void UpdateYZXProperties() { string s = HSI.GenYZXProperties(); YZXProperties = s; HSI.FireOnObjectChanged(AffectedCategories.Property, "YZXProperties", s, new CoreObjectEventArgs(HSI.CoreObject) ); ScreenItemManager manager = (ScreenItemManager)HSI.Manager; var parent = HSI.Parent; var parentofhmiscreenItem = HSI.ParentOfHmiScreenItem; }
public HSI ToHSI() { HSI hsi = new HSI(); double r = color.R / 255.0; double g = color.G / 255.0; double b = color.B / 255.0; double theta = Math.Acos(0.5 * ((r - g) + (r - b)) / Math.Sqrt((r - g) * (r - g) + (r - b) * (g - b))) / (2 * Math.PI); hsi.Hue = (b <= g) ? theta : (1 - theta); hsi.Saturation = 1 - 3 * Math.Min(Math.Min(r, g), b) / (r + g + b); hsi.Intensity = (r + g + b) / 3; return(hsi); }
public HSI to_HSI(RGB rgb) { HSI hsi = new HSI(); double r = (rgb.Red / 255.0); double g = (rgb.Green / 255.0); double b = (rgb.Blue / 255.0); double theta = Math.Acos(0.5 * ((r - g) + (r - b)) / Math.Sqrt((r - g) * (r - g) + (r - b) * (g - b))) / (2 * Math.PI); hsi.Hue = (b <= g) ? theta : (1 - theta); hsi.Saturation = 1 - 3 * Math.Min(Math.Min(r, g), b) / (r + g + b); hsi.Intensity = (r + g + b) / 3; return(hsi); }
/// <summary> /// Convert RGB colorspace to HSI colorspace /// </summary> /// <param name="rgb">Input RGB pixel</param> /// <returns>HSI colorspace pixel</returns> public static HSI RGB2HSI(RGB rgb) { var c = rgb.Color; HSI hsi = new HSI(c.GetHue(), c.GetSaturation(), c.GetBrightness()); //double r = (rgb.Red / 255.0); //double g = (rgb.Green / 255.0); //double b = (rgb.Blue / 255.0); //double theta = Math.Acos(0.5 * ((r - g) + (r - b)) / Math.Sqrt((r - g) * (r - g) + (r - b) * (g - b))) / (2 * Math.PI); //hsi.Hue = (b <= g) ? theta : (1 - theta); //hsi.Saturation = 1 - 3 * Math.Min(Math.Min(r, g), b) / (r + g + b); //hsi.Intensity = (r + g + b) / 3; return(hsi); }
public HSI to_HSI(RGB rgb) { HSI hsi = new HSI(); if (rgb.Red == 255 && rgb.Green == 255 && rgb.Blue == 255) { hsi.Hue = 0; hsi.Saturation = 0; hsi.Intensity = 1; } else if (rgb.Red == 0 && rgb.Green == 0 && rgb.Blue == 0) { hsi.Hue = 0; hsi.Saturation = 0; hsi.Intensity = 0; } else if (rgb.Red == rgb.Blue && rgb.Red == rgb.Green) { hsi.Hue = 0; hsi.Saturation = 0; hsi.Intensity = rgb.Green / 255.0; } else { double r = (rgb.Red / 255.0); double g = (rgb.Green / 255.0); double b = (rgb.Blue / 255.0); double theta = Math.Acos(0.5 * ((r - g) + (r - b)) / Math.Sqrt((r - g) * (r - g) + (r - b) * (g - b))) / (2 * Math.PI); hsi.Hue = (b <= g) ? theta : (1 - theta); hsi.Saturation = 1 - 3 * Math.Min(Math.Min(r, g), b) / (r + g + b); hsi.Intensity = (r + g + b) / 3; } return(hsi); }
public static Color hsi2Rgb(HSI hsi) { double x, y, z; double r, g, b; double h = hsi.Hue, s = hsi.Saturation, i = hsi.Intensity; //hsi反归一化 h = h * 2 * Math.PI; s = s * (0xff * Math.Sqrt(2.0 / 3.0)); i = i * Math.Sqrt(0xff * 0xff + 0xff * 0xff + 0xff * 0xff); //hsi -> xyz x = s * Math.Cos(h); y = s * Math.Sin(h); z = i; //xyz -> rgb r = x * Math.Sqrt(2.0 / 3.0) + z / Math.Sqrt(3); g = -x / 2 + y * Math.Sqrt(3) / 2 + z / Math.Sqrt(3); b = -x / 2 - y * Math.Sqrt(3) / 2 + z / Math.Sqrt(3); //限制rgb超出范围 r = Math.Max(0, Math.Min(0xff, r)); g = Math.Max(0, Math.Min(0xff, g)); b = Math.Max(0, Math.Min(0xff, b)); return(Color.FromArgb(0xff, Convert.ToByte(r), Convert.ToByte(g), Convert.ToByte(b))); }
/// <summary> /// 更新当前获取的颜色 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void colortimer_Tick(object sender, EventArgs e) { if (!stopDrawPreview && previewForm.MouseOnMe) { // 如果没有停止预览,并且鼠标在预览窗口上 // 就不取色,这是为了防止因循环取色导致过高的资源占用 return; } if (!settingLoaded) { // 不晓得为啥,在启动时加载Visible会被覆盖,所在放到这里来了 SwitchDisplayMode(Settings.Main.Display); settingLoaded = true; } var color = ColorUtil.GetColor(MousePosition); // 如果光标位置不变,颜色也不变,就不绘制了 if (MousePosition.Equals(lastPosition) && color.Equals(lastColor)) { return; } lastPosition = MousePosition; lastColor = color; colorBuffer.Clear(); var val = colorBuffer.AppendFormat("{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B).ToString(); lbHex.Tag = val; colorBuffer.Clear(); lbHex.Text = colorBuffer.AppendFormat("#{0}", lbHex.Tag).ToString(); colorBuffer.Clear(); val = colorBuffer.AppendFormat("{0},{1},{2}", color.R, color.G, color.B).ToString(); lbRgb.Tag = val; colorBuffer.Clear(); lbRgb.Text = colorBuffer.AppendFormat("RGB({0})", lbRgb.Tag).ToString(); colorBuffer.Clear(); if (trayMenuShowPreview.Checked && !stopDrawPreview) { DrawPreview(MousePosition); } if (FormatMode.Mini == currentFormatMode) { if (!lbColorPreview.Visible) { lbColorPreview.Show(); } lbColorPreview.BackColor = color; return; } if (lbColorPreview.Visible) { lbColorPreview.Hide(); } var contrastColor = ColorUtil.GetContrastColor(color); if (FormatMode.Standard == currentFormatMode) { lbRgb.BackColor = color; lbRgb.ForeColor = contrastColor; return; } if (lbRgb.BackColor != BackColor) { lbRgb.BackColor = BackColor; lbRgb.ForeColor = ForeColor; } // var hsl = HSL.Parse(color); var hsl = new HSL(color.GetHue(), color.GetSaturation(), color.GetBrightness()); lbHsl.Text = colorBuffer.AppendFormat("HSL({0},{1},{2})", Math.Round(hsl.H), Util.Round(hsl.S * 100), Util.Round(hsl.L * 100)).ToString(); colorBuffer.Clear(); var hsb = HSB.Parse(color); lbHsb.Text = colorBuffer.AppendFormat("HSB({0},{1},{2})", Math.Round(hsb.H), Util.Round(hsb.S * 100), Util.Round(hsb.B * 100)).ToString(); colorBuffer.Clear(); var hsi = HSI.Parse(color, currentHsiAlgorithm); lbHsi.Text = colorBuffer.AppendFormat("HSI({0},{1},{2})", Math.Round(hsi.H), Util.Round(hsi.S * 100), Util.Round(hsi.I * 100)).ToString(); colorBuffer.Clear(); pnExt.BackColor = color; pnExt.ForeColor = contrastColor; }
public RGB to_RGB(HSI hsi) { RGB rgb = new RGB(); double r, g, b; double h = hsi.Hue; double s = hsi.Saturation; double i = hsi.Intensity; h = h * 2 * Math.PI; if (h >= 0 && h < 2 * Math.PI / 3) { b = i * (1 - s); r = i * (1 + s * Math.Cos(h) / Math.Cos(Math.PI / 3 - h)); g = 3 * i - (r + b); } else if (h >= 2 * Math.PI / 3 && h < 4 * Math.PI / 3) { r = i * (1 - s); g = i * (1 + s * Math.Cos(h - 2 * Math.PI / 3) / Math.Cos(Math.PI - h)); b = 3 * i - (r + g); } else //if (h >= 4 * Math.PI / 3 && h <= 2 * Math.PI) { g = i * (1 - s); b = i * (1 + s * Math.Cos(h - 4 * Math.PI / 3) / Math.Cos(5 * Math.PI / 3 - h)); r = 3 * i - (g + b); } r *= 255; g *= 255; b *= 255; if (r > 255) { r = 255; } else if (r < 0) { r = 0; } if (g > 255) { g = 255; } else if (g < 0) { g = 0; } if (b > 255) { b = 255; } else if (b < 0) { b = 0; } rgb.Red = Convert.ToInt32(r); rgb.Blue = Convert.ToInt32(b); rgb.Green = Convert.ToInt32(g); return(rgb); }
private void BtnCalculate_Click(object sender, EventArgs e) { i = 0; #region InputFromTbx CbxSelectIndexValue(); try { holeDepth = 0; casingId = 0; casingShoe = 0; mudWeight = 0; flowRate = 0; PV = 0; YP = 0; dcLength = 0; dcId = 0; dcOd = 0; hwdpLength = 0; hwdpId = 0; hwdpOd = 0; surfacePressureLoss = 0; mudMoter = 0; dpLength = 0; dpOd = 0; dpId = 0; if (!string.IsNullOrEmpty(tbxHole.Text)) { holeDepth = Convert.ToDouble(tbxHole.Text); } if (!string.IsNullOrEmpty(tbxCasingID.Text)) { casingId = Convert.ToDouble(tbxCasingID.Text); } if (!string.IsNullOrEmpty(tbxCasingShoe.Text)) { casingShoe = Convert.ToDouble(tbxCasingShoe.Text); } if (!string.IsNullOrEmpty(tbxMudWeight.Text)) { mudWeight = Convert.ToDouble(tbxMudWeight.Text); } if (!string.IsNullOrEmpty(tbxFlowRate.Text)) { flowRate = Convert.ToDouble(tbxFlowRate.Text); } if (!string.IsNullOrEmpty(tbxPV.Text)) { PV = Convert.ToDouble(tbxPV.Text); } if (!string.IsNullOrEmpty(tbxYP.Text)) { YP = Convert.ToDouble(tbxYP.Text); } if (!string.IsNullOrEmpty(tbxDcLength.Text)) { dcLength = Convert.ToDouble(tbxDcLength.Text); } if (!string.IsNullOrEmpty(tbxDcId.Text)) { dcId = Convert.ToDouble(tbxDcId.Text); } if (!string.IsNullOrEmpty(tbxDcOd.Text)) { dcOd = Convert.ToDouble(tbxDcOd.Text); } if (!string.IsNullOrEmpty(tbxHwdpLength.Text)) { hwdpLength = Convert.ToDouble(tbxHwdpLength.Text); } if (!string.IsNullOrEmpty(tbxHwdpId.Text)) { hwdpId = Convert.ToDouble(tbxHwdpId.Text); } if (!string.IsNullOrEmpty(tbxHwdpOd.Text)) { hwdpOd = Convert.ToDouble(tbxHwdpOd.Text); } if (!string.IsNullOrEmpty(tbxDpId.Text)) { dpId = Convert.ToDouble(tbxDpId.Text); } if (!string.IsNullOrEmpty(tbxDpOd.Text)) { dpOd = Convert.ToDouble(tbxDpOd.Text); } if (!string.IsNullOrEmpty(tbxDpLength.Text)) { dpLength = Convert.ToDouble(tbxDpLength.Text); } if (!string.IsNullOrEmpty(tbxMudMoter.Text)) { mudMoter = Convert.ToDouble(tbxMudMoter.Text); } if (!string.IsNullOrEmpty(tbxSurfacePressure.Text)) { surfacePressureLoss = Convert.ToDouble(tbxSurfacePressure.Text); } } catch { } #endregion TryCatchNozzle(); Calculate calculate = new Calculate(); tbxResultSurfacePressure.Text = surfacePressureLoss.ToString(); tbxResultSurfacePressure.ForeColor = Color.Blue; tfaResult = Math.Round(calculate.CalculateResult(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12), 3); lblResultTotalFlow.Text = tfaResult.ToString(); lblResultTotalFlow.ForeColor = Color.Blue; bitPressureLossResult = Math.Round(calculate.CalculateBitPressure(mudWeight, flowRate, tfaResult)); tbxResultBitPressure.Text = bitPressureLossResult.ToString(); tbxResultBitPressure.ForeColor = Color.Blue; HHP = Math.Round(calculate.CalculateHHP(bitPressureLossResult, flowRate)); tbxResultHHP.Text = HHP.ToString(); tbxResultHHP.ForeColor = Color.Blue; HSI = Math.Round(calculate.CalculateHSI(HHP, cbxValue), 2); tbxResultHSI.Text = HSI.ToString(); tbxResultHSI.ForeColor = Color.Blue; JIF = Math.Round(calculate.JIF(flowRate, mudWeight, bitPressureLossResult)); tbxResultJIF.Text = JIF.ToString(); tbxResultJIF.ForeColor = Color.Blue; JIFin2 = Math.Round(calculate.JIFin2(JIF, cbxValue), 2); tbxResultJIFin2.Text = JIFin2.ToString(); tbxResultJIFin2.ForeColor = Color.Blue; nozzleVelocity = Math.Round(calculate.NoozleResult(flowRate, tfaResult)); tbxResultNoozle.Text = nozzleVelocity.ToString(); tbxResultNoozle.ForeColor = Color.Blue; drillStringLoss = Math.Round(calculate.DrillStringLosses(mudWeight, flowRate, PV, hwdpLength, hwdpId, dcLength, dcId, dpLength, dpId)); tbxResultDrillString.Text = drillStringLoss.ToString(); tbxResultDrillString.ForeColor = Color.Blue; annularPressure = Math.Round(calculate.AnnularPressure(hwdpLength, mudWeight, flowRate, cbxValue, hwdpOd, PV, YP, dpLength, dpOd, dcLength, dcOd)); tbxResultAnnular.Text = annularPressure.ToString(); tbxResultAnnular.ForeColor = Color.Blue; ECD = Math.Round(calculate.ECD(annularPressure, holeDepth, mudWeight), 2); tbxResultECD.Text = ECD.ToString(); tbxResultECD.ForeColor = Color.Blue; SPP = calculate.SPP(surfacePressureLoss, drillStringLoss, bitPressureLossResult, annularPressure, mudMoter); tbxResultSPP.Text = Math.Round(SPP).ToString(); tbxResultSPP.ForeColor = Color.Blue; btnPDF.Enabled = true; }
private void histogramEqualizationToolStripMenuItem_Click(object sender, EventArgs e) { Bitmap source = (Bitmap)pictureBox1.Image; int[] graylevel = new int[256]; HSI[,] hsi = new HSI[source.Height, source.Width]; progressBar1.Minimum = 0; progressBar1.Maximum = (source.Width * source.Height * 4); progressBar1.Value = 0; for (int i = 0; i < source.Height; i++) { for (int j = 0; j < source.Width; j++) { RGB rgb = new RGB(source.GetPixel(j, i).R, source.GetPixel(j, i).G, source.GetPixel(j, i).B); hsi[i, j] = to_HSI(rgb); progressBar1.Increment(1); } } //// for (int i = 0; i < source.Height; i++) { for (int j = 0; j < source.Width; j++) { int inx = Convert.ToInt32(hsi[i, j].Intensity * 255); graylevel[inx]++; progressBar1.Increment(1); } } double[] Transform = new double[256]; for (int i = 0; i < 256; i++) { Transform[i] = (double)graylevel[i] / (double)(source.Height * source.Width); } for (int i = 1; i < 256; i++) { Transform[i] = Transform[i] + Transform[i - 1]; } for (int i = 0; i < source.Height; i++) { for (int j = 0; j < source.Width; j++) { int inx = Convert.ToInt32(hsi[i, j].Intensity * 255); hsi[i, j].Intensity = Transform[inx]; progressBar1.Increment(1); } } //// Bitmap result = new Bitmap(source.Width, source.Height); for (int i = 0; i < source.Height; i++) { for (int j = 0; j < source.Width; j++) { RGB rgb = new RGB(); rgb = to_RGB(hsi[i, j]); result.SetPixel(j, i, Color.FromArgb(rgb.Red, rgb.Green, rgb.Blue)); progressBar1.Increment(1); } } progressBar1.Value = 0; pictureBox2.Image = result; }