public override void OnMouseUp(Vector2d pos) { if (_drawingbox) { _drawingbox = false; if (_selection.Count == 0 && _boxselection.Count == 0) { Stop(true); } else { foreach (var v in _boxselection) { _selectedlines.Add(v.line.ID); _selection.Add(v); } _selectionbox = GetBoxFromSelected(_selection); _boxselection.Clear(); } _drawbox = DoubleRect.Empty; } else { ReleaseSelection(); } base.OnMouseUp(pos); }
public static void RenderRoundedRectangle(DoubleRect rect, Color color, float thickness, bool gamecoords = true) { using (new GLEnableCap(EnableCap.Blend)) { using (new GLEnableCap(EnableCap.Texture2D)) { if (gamecoords) { GameDrawingMatrix.Enter(); } var vao = GetLineVAO(); vao.Scale = GameDrawingMatrix.Scale; var vec1 = rect.Vector; var vec2 = vec1 + new Vector2d(rect.Width, 0); var vec3 = vec1 + rect.Size; var vec4 = vec1 + new Vector2d(0, rect.Height); vao.AddLine(vec1, vec2, color, thickness); vao.AddLine(vec2, vec3, color, thickness); vao.AddLine(vec3, vec4, color, thickness); vao.AddLine(vec4, vec1, color, thickness); vao.knobstate = 0; vao.Draw(PrimitiveType.Triangles); if (gamecoords) { GameDrawingMatrix.Exit(); } } } }
private void Stop(bool defertomovetool) { if (Active) { SaveMovedSelection(); } Active = false; UnselectBox(); Unselect(); CancelDrawBox(); _selectionbox = DoubleRect.Empty; _movingselection = false; _movemade = false; _snapline = null; _hoverline = null; _snapknob1 = false; _snapknob2 = false; _scaling = false; _rotating = false; _hoverscale = false; if (defertomovetool) { DeferToMoveTool(); } }
private bool ToggleSelection(Vector2d gamepos) { using (var trk = game.Track.CreateTrackWriter()) { var line = SelectLine(trk, gamepos, out bool knob); if (line != null) { if (_selectedlines.Contains(line.ID)) { _selectedlines.Remove(line.ID); _selection.RemoveAt( _selection.FindIndex( x => x.line.ID == line.ID)); line.SelectionState = SelectionState.None; } else { _selectedlines.Add(line.ID); var selection = new LineSelection(line, true, null); _selection.Add(selection); line.SelectionState = SelectionState.Selected; } _selectionbox = GetBoxFromSelected(_selection); game.Track.RedrawLine(line); return(true); } } return(false); }
public override void OnUndoRedo(bool isundo, object undohint) { if (Active && (_selection.Count != 0 || _boxselection.Count != 0) && (undohint is int[] lineids)) { if (lineids.Length != 0) { Stop(false); _hoverline = null; Active = true; using (var trk = game.Track.CreateTrackWriter()) { foreach (var lineid in lineids) { var line = trk.Track.LineLookup[lineid]; _selectedlines.Add(line.ID); var selection = new LineSelection(line, true, null); _selection.Add(selection); line.SelectionState = SelectionState.Selected; game.Track.RedrawLine(line); } } _selectionbox = GetBoxFromSelected(_selection); return; } } Stop(true); }
public override void OnMouseDown(Vector2d pos) { var gamepos = ScreenToGameCoords(pos); if (Active && _selection.Count != 0) { if (StartTransformSelection(gamepos, false)) { return; } if (UI.InputUtils.CheckPressed(UI.Hotkey.ToolAddSelection)) { StartAddSelection(gamepos); return; } else if (UI.InputUtils.CheckPressed(UI.Hotkey.ToolToggleSelection)) { ToggleSelection(gamepos); return; } else if (StartMoveSelection(gamepos)) { return; } } Unselect(); _selectionbox = DoubleRect.Empty; _drawbox = new DoubleRect(gamepos, Vector2d.Zero); Active = true; _drawingbox = true; _movingselection = false; base.OnMouseDown(pos); }
private void DrawTexture( Tex tex, DoubleRect rect, FloatRect uv, Vector2d origin, Vector2d rotationAnchor, float opacity, bool pretty = false ) { var angle = Angle.FromLine(origin, rotationAnchor); var t = Utility.RotateRect(rect, Vector2d.Zero, angle); var transform = new Vector2d[] { (t[0] + origin), // 0 tl (t[1] + origin), // 1 tr (t[2] + origin), // 2 br (t[3] + origin), // 3 bl }; var texrect = GameDrawingMatrix.ScreenCoords(transform); var color = Color.FromArgb((int)(255f * opacity), Color.White); Color[] colors = new Color[] { color, color, color, color }; if (pretty) { var random = new Random((Environment.TickCount / 100) % 255); for (int i = 0; i < colors.Length; i++) { bool redness = random.Next() % 2 == 0; bool blueness = random.Next() % 2 == 0; var random1 = Math.Max(200, random.Next() % 255); int red = Math.Min(255, redness ? (random1 * 2) : (random1 / 2)); int green = Math.Min(255, (blueness && redness) ? (random1) : (random1 / 2)); int blue = Math.Min(255, blueness ? (random1 * 2) : (random1 / 2)); colors[i] = Color.FromArgb((int)(255f * opacity), red, green, blue); } } var u1 = uv.Left; var v1 = uv.Top; var u2 = uv.Right; var v2 = uv.Bottom; var verts = new RiderVertex[] { new RiderVertex(texrect[0], new Vector2(uv.Left, uv.Top), tex, colors[0]), new RiderVertex(texrect[1], new Vector2(uv.Right, uv.Top), tex, colors[1]), new RiderVertex(texrect[2], new Vector2(uv.Right, uv.Bottom), tex, colors[2]), new RiderVertex(texrect[3], new Vector2(uv.Left, uv.Bottom), tex, colors[3]) }; Array.Add(verts[0]); Array.Add(verts[1]); Array.Add(verts[2]); Array.Add(verts[3]); Array.Add(verts[2]); Array.Add(verts[0]); }
private void RenderCorner(DoubleRect box, bool top, bool left) { var corner = GetCorner(box, top, left); var rect = new DoubleRect( GameDrawingMatrix.ScreenCoordD(corner.Vector), new Vector2d(corner.Width * game.Track.Zoom, corner.Height * game.Track.Zoom)); GameRenderer.RenderRoundedRectangle(rect, Color.CornflowerBlue, 2, false); }
public bool CancelDrawBox() { if (_drawingbox) { UnselectBox(); _drawingbox = false; _drawbox = DoubleRect.Empty; return(true); } return(false); }
public void SetupLegacy(float zoom) { var width = (double)game.RenderSize.Width; var height = width * (9.0 / 16.0);//16:9 camera height /= game.Track.Zoom; width /= game.Track.Zoom; height *= legacyratio; width *= legacyratio; Bounds = new DoubleRect(RiderPosition.X - (width / 2), RiderPosition.Y - (height / 2), width, height); _initialized = true; _smooth = false; }
void Redraw() { if (Chart == null) { return; } DoubleVector3 last; if (Chart.DataSource.GetLastPoint("Player 1", out last)) { if (TrackLast == false) { last.y = yPosition; } Vector3 pos; DoubleRect rect = new DoubleRect(last.x - Chart.DataSource.HorizontalViewSize, last.y - lineThickness * 0.5f, Chart.DataSource.HorizontalViewSize, lineThickness); DoubleRect trimRect; if (Area != null) { Vector3 res; if (Chart.PointToWorldSpace(out res, rect.min.x, rect.min.y)) { res.y += lineThickness; double x, y; if (Chart.PointToClient(res, out x, out y)) { double thick = Math.Abs(rect.min.y - y); rect = new DoubleRect(last.x - Chart.DataSource.HorizontalViewSize, last.y - thick * 0.5f, Chart.DataSource.HorizontalViewSize, thick); } } if (Chart.TrimRect(rect, out trimRect)) // if the rect is in display { if (Area.gameObject.activeSelf == false) // we draw it { Area.gameObject.SetActive(true); } Chart.RectToCanvas(Area, trimRect); // Area.sizeDelta = new Vector2(Area.sizeDelta.x, lineThickness); } else { if (Area.gameObject.activeSelf == true) // otherwise it is set as incative { Area.gameObject.SetActive(false); } } } } }
public void SetupSmooth(double ppf, float zoom) { var scale = GetSmoothCamRatio(ppf); var width = (double)game.RenderSize.Width; var height = width * (9.0 / 16.0);//16:9 camera height /= game.Track.Zoom; width /= game.Track.Zoom; height *= scale; width *= scale; Bounds = new DoubleRect(RiderPosition.X - (width / 2), RiderPosition.Y - (height / 2), width, height); _initialized = true; _smooth = true; }
public void Intersect(DoubleRect drc) { left = Math.Max(left, drc.left); right = Math.Min(right, drc.right); if (left < right) { top = Math.Max(top, drc.top); bottom = Math.Min(bottom, drc.bottom); if (top < bottom) return; } left = 0.0; right = 0.0; top = 0.0; bottom = 0.0; }
public static void RenderPoints(List <Vector2d> points, BezierCurve curve, Color color, float nodeSize) { for (int i = 0; i < points.Count; i++) { if (i == 0 || i == points.Count - 1) { DoubleRect rect = new DoubleRect(points[i].X - nodeSize, points[i].Y - nodeSize, nodeSize * 2, nodeSize * 2); RenderRoundedRectangle(rect, color, 1); } else { DrawCircle(points[i], nodeSize, color); } } renderPointTrace(points, curve, color); }
public static DoubleRect GetBounds(Rider r) { double left, right, top, bottom; right = left = r.Body[0].Location.X; top = bottom = r.Body[0].Location.Y; for (int i = 0; i < r.Body.Length; i++) { var pos = r.Body[i].Location; right = Math.Max(pos.X, right); left = Math.Min(pos.X, left); top = Math.Min(pos.Y, top); bottom = Math.Max(pos.Y, bottom); } DoubleRect ret = new DoubleRect(left, top, right - left, bottom - top); return(ret); }
public static void DrawTexture(int tex, DoubleRect rect, float alpha = 1, float u1 = 0, float v1 = 0, float u2 = 1, float v2 = 1) { GenericVAO buf = new GenericVAO(); var tr = new Vector2d(rect.Right, rect.Top); var tl = new Vector2d(rect.Left, rect.Top); var bl = new Vector2d(rect.Left, rect.Bottom); var br = new Vector2d(rect.Right, rect.Bottom); var c = Color.FromArgb((int)Math.Min(255, (alpha * 255)), 255, 255, 255); buf.AddVertex(new GenericVertex((Vector2)tl, c, u1, v1)); buf.AddVertex(new GenericVertex((Vector2)tr, c, u2, v1)); buf.AddVertex(new GenericVertex((Vector2)br, c, u2, v2)); buf.AddVertex(new GenericVertex((Vector2)bl, c, u1, v2)); using (new GLEnableCap(EnableCap.Texture2D)) using (new GLEnableCap(EnableCap.Blend)) { GL.BindTexture(TextureTarget.Texture2D, tex); buf.Draw(PrimitiveType.Quads); GL.BindTexture(TextureTarget.Texture2D, 0); } }
public List <LineSelection> SelectLines(List <GameLine> lines, bool additive = false) { if (!additive) { Unselect(); UnselectBox(); } foreach (GameLine line in lines) { LineSelection selection = new LineSelection(line, true, null); _selectedlines.Add(line.ID); _selection.Add(selection); _boxselection.Add(selection); line.SelectionState = SelectionState.Selected; game.Track.RedrawLine(line); } _selectionbox = GetBoxFromSelected(_selection); Render(); return(_selection); }
void Redraw() { if (Chart == null) { return; } if (Chart.IsRectVisible(currentRect) == false) { double endX = (float)(Chart.HorizontalScrolling + Chart.DataSource.HorizontalViewSize); double x = endX - 1f; double y = (float)Chart.VerticalScrolling; double endY = (float)Chart.DataSource.GetMaxValue(1, false); currentRect = new DoubleRect(x, y, endX - x, endY - y); } DoubleRect trimRect; if (Chart.TrimRect(currentRect, out trimRect)) { Chart.RectToCanvas(Area, trimRect); } DoubleVector3 last; if (Chart.DataSource.GetLastPoint("Player 1", out last)) { Vector3 pos; if (Chart.PointToWorldSpace(out pos, last.x, last.y, "Player 1")) { if (LastPoint != null) { LastPoint.transform.position = pos; } } } }
public void Paste() { if (_copybuffer.Count != 0) { Stop(false); var pasteorigin = GetCopyOrigin(); var diff = pasteorigin - _copyorigin; Unselect(); Active = true; if (CurrentTools.SelectedTool != this) { CurrentTools.SetTool(this); } using (var trk = game.Track.CreateTrackWriter()) { game.Track.UndoManager.BeginAction(); foreach (var line in _copybuffer) { var add = line.Clone(); add.ID = GameLine.UninitializedID; add.Position += diff; add.Position2 += diff; if (add is StandardLine stl) { stl.CalculateConstants(); } add.SelectionState = SelectionState.Selected; trk.AddLine(add); var selectinfo = new LineSelection(add, true, null); _selection.Add(selectinfo); _selectedlines.Add(add.ID); } game.Track.UndoManager.EndAction(); } _selectionbox = GetBoxFromSelected(_selection); game.Track.NotifyTrackChanged(); } }
/// <param name="rotate"> /// If true rotates selection, if false scales selection /// </param> private bool StartTransformSelection(Vector2d gamepos, bool rotate) { if (CornerContains(gamepos, _nodetop = true, _nodeleft = true) || CornerContains(gamepos, _nodetop = true, _nodeleft = false) || CornerContains(gamepos, _nodetop = false, _nodeleft = false) || CornerContains(gamepos, _nodetop = false, _nodeleft = true)) { if (rotate) { _rotating = true; } else { _scaling = true; } _movingselection = true; _clickstart = gamepos; _boxstart = _selectionbox; _startselectionedges = GetBoxLineEdges(_selection); return(true); } return(false); }
public LineContainer<GameLine> LinesInRect(DoubleRect rect) { int starty = (int)Math.Floor(rect.Top / CellSize); int startx = (int)Math.Floor(rect.Left / CellSize); int endy = (int)Math.Floor((rect.Top + rect.Height) / CellSize); int endx = (int)Math.Floor((rect.Left + rect.Width) / CellSize); LineContainer<GameLine> ret = new LineContainer<GameLine>(); using (Sync.AcquireWrite()) { for (int x = startx; x <= endx; x++) { for (int y = starty; y <= endy; y++) { var cell = GetCell(x, y); if (cell != null) { ret.Combine(cell); } } } } return ret; }
private void RotateSelection(Vector2d pos) { if (_selection.Count > 0) { _movemade = true; var movediff = (pos - _clickstart); using (var trk = game.Track.CreateTrackWriter()) { trk.DisableUndo(); var center = _startselectionedges.Vector + (_startselectionedges.Size / 2); var edge = new Vector2d(_nodeleft ? _startselectionedges.Left : _startselectionedges.Right, _nodetop ? _startselectionedges.Top : _startselectionedges.Bottom); var angle = Angle.FromVector(edge - center); var newangle = Angle.FromVector(pos - center); var anglediff = newangle - angle; if (game.ShouldXySnap()) { anglediff = new Angle(Math.Round(anglediff.Degrees / 15) * 15); } foreach (var selected in _selection) { Vector2d p1 = Utility.Rotate(selected.clone.Position, center, anglediff); Vector2d p2 = Utility.Rotate(selected.clone.Position2, center, anglediff); trk.MoveLine(selected.line, p1, p2); } _selectionbox = GetBoxFromSelected(_selection); game.Track.NotifyTrackChanged(); } } game.Invalidate(); }
private bool StartMoveSelection(Vector2d gamepos) { if (_selectionbox.Contains(gamepos.X, gamepos.Y)) { using (var trk = game.Track.CreateTrackReader()) { var selected = SelectInSelection(trk, gamepos); if (selected != null) { bool snapped = IsLineSnappedByKnob(trk, gamepos, selected.clone, out bool knob1); _snapknob1 = !snapped || knob1; _snapknob2 = !snapped || !knob1; _snapline = selected.clone; _clickstart = gamepos; _boxstart = _selectionbox; _movingselection = true; return(true); } } } return(false); }
private DoubleRect GetCorner(DoubleRect box, bool top, bool left, int boxsize = 15) { DoubleRect corner = box; corner.Width = boxsize / game.Track.Zoom; corner.Height = boxsize / game.Track.Zoom; if (!left) { corner.Left = box.Right; } else { corner.Left -= corner.Width; } if (!top) { corner.Top = box.Bottom; } else { corner.Top -= corner.Height; } return(corner); }
private void ScaleSelection(Vector2d pos) { if (_selection.Count > 0) { _movemade = true; var movediff = (pos - _clickstart); if (UI.InputUtils.CheckPressed(UI.Hotkey.ToolScaleAspectRatio)) { if (_nodeleft == _nodetop) { movediff.Y = movediff.X * (_boxstart.Height / _boxstart.Width); } else { movediff.Y = -(movediff.X * (_boxstart.Height / _boxstart.Width)); } } using (var trk = game.Track.CreateTrackWriter()) { trk.DisableUndo(); foreach (var selected in _selection) { var scalar1 = Vector2d.Divide( selected.clone.Position - _startselectionedges.Vector, _startselectionedges.Size); var scalar2 = Vector2d.Divide( selected.clone.Position2 - _startselectionedges.Vector, _startselectionedges.Size); if (_nodetop) { scalar1.Y = 1 - scalar1.Y; scalar2.Y = 1 - scalar2.Y; } if (_nodeleft) { scalar1.X = 1 - scalar1.X; scalar2.X = 1 - scalar2.X; } if (_startselectionedges.Size.X == 0) { scalar1.X = 0; scalar2.X = 0; } if (_startselectionedges.Size.Y == 0) { scalar1.Y = 0; scalar2.Y = 0; } var p1 = Vector2d.Multiply(scalar1, movediff); var p2 = Vector2d.Multiply(scalar2, movediff); trk.MoveLine( selected.line, selected.clone.Position + p1, selected.clone.Position2 + p2); } _selectionbox = GetBoxFromSelected(_selection); game.Track.NotifyTrackChanged(); } } game.Invalidate(); }
private void StartAddSelection(Vector2d gamepos) { _movingselection = false; _drawbox = new DoubleRect(gamepos, Vector2d.Zero); _drawingbox = true; }
public static Bitmap ScaleTemplateBitmap(Bitmap bm, Bitmap bmMask, int cxNew, int cyNew, double nAreaBackgroundThreshold, double nLuminanceMultBackground, double nSaturationMultBackground, double nLuminanceMultForeground, double nSaturationMultForeground) { Bitmap bmNew = new Bitmap(cxNew, cyNew); for (int y = 0; y < bmNew.Height; y++) { for (int x = 0; x < bmNew.Width; x++) { double xLeft = (double)x * (double)bm.Width / (double)bmNew.Width; double yTop = (double)y * (double)bm.Height / (double)bmNew.Height; double xRight = (double)(x + 1) * (double)bm.Width / (double)bmNew.Width; if (xRight > bm.Width) xRight = bm.Width; double yBottom = (double)(y + 1) * (double)bm.Height / (double)bmNew.Height; if (yBottom > bm.Height) yBottom = bm.Height; DoubleRect drc = new DoubleRect(xLeft, yTop, xRight, yBottom); Color clrSample = SampleTemplateBitmap(bm, bmMask, drc, nAreaBackgroundThreshold, nLuminanceMultBackground, nSaturationMultBackground, nLuminanceMultForeground, nSaturationMultForeground); bmNew.SetPixel(x, y, clrSample); } } return bmNew; }
public static void DbgDrawCamera() { GL.PushMatrix(); var center = new Vector2(Game.RenderSize.Width / 2, Game.RenderSize.Height / 2); var rect = Game.Track.Camera.getclamp(1, Game.RenderSize.Width, Game.RenderSize.Height); rect.Width *= Game.Track.Zoom; rect.Height *= Game.Track.Zoom; var circle = StaticRenderer.GenerateEllipse((float)rect.Width, (float)rect.Height, 100); var clamprect = new DoubleRect(center.X, center.Y, 0, 0); clamprect.Left -= rect.Width / 2; clamprect.Top -= rect.Height / 2; clamprect.Width = rect.Width; clamprect.Height = rect.Height; if (!Settings.SmoothCamera && !Settings.RoundLegacyCamera) { GL.Begin(PrimitiveType.LineStrip); GL.Color3(0, 0, 0); GL.Vertex2(clamprect.Left, clamprect.Top); GL.Vertex2(clamprect.Right, clamprect.Top); GL.Vertex2(clamprect.Right, clamprect.Bottom); GL.Vertex2(clamprect.Left, clamprect.Bottom); GL.Vertex2(clamprect.Left, clamprect.Top); GL.End(); GL.PopMatrix(); return; } GL.Begin(PrimitiveType.LineStrip); GL.Color3(0, 0, 0); for (int i = 0; i < circle.Length; i++) { var pos = (Vector2d)center + (Vector2d)circle[i]; var square = clamprect.Clamp(pos); var oval = clamprect.EllipseClamp(pos); pos = (Vector2d.Lerp(square, oval, CameraBoundingBox.roundness)); GL.Vertex2(pos); } GL.End(); // visualize example points being clamped GL.Begin(PrimitiveType.Lines); circle = StaticRenderer.GenerateEllipse((float)rect.Width / 1.5f, (float)rect.Height / 1.5f, 20); for (int i = 0; i < circle.Length; i++) { var pos = (Vector2d)center + (Vector2d)circle[i]; var square = clamprect.Clamp(pos); var oval = clamprect.EllipseClamp(pos); pos = (Vector2d.Lerp(square, oval, CameraBoundingBox.roundness)); if (pos != (Vector2d)center + (Vector2d)circle[i]) { GL.Vertex2(pos); GL.Vertex2((Vector2d)center + (Vector2d)circle[i]); } } GL.End(); GL.PopMatrix(); //visualize rider center // DrawCircle(Game.Track.Camera.GetSmoothPosition(), 5, Color.Red); // DrawCircle(Game.Track.Camera.GetSmoothedCameraOffset(), 5, Color.Blue); DrawCircle(Game.Track.Timeline.GetFrame(Game.Track.Offset).CalculateCenter(), 5, Color.Green); }
/// DoubleRectの拡張: Rectへ変換 public static Rect ToRect(this DoubleRect rect) { return(new Rect(rect.X, rect.Y, rect.Width, rect.Height)); }
static void ScaleTile(Bitmap bmSrc, Rectangle rcSrc, Bitmap bmDst, Rectangle rcDst) { double nWidthRatio = (double)rcSrc.Width / (double)rcDst.Width; double nHeightRatio = (double)rcSrc.Height / (double)rcDst.Height; for (int yDst = rcDst.Top; yDst < rcDst.Bottom; yDst++) { for (int xDst = rcDst.Left; xDst < rcDst.Right; xDst++) { double xLeftSample = (double)xDst * nWidthRatio; double yTopSample = (double)yDst * nHeightRatio; double xRightSample = (double)(xDst + 1) * nWidthRatio; double yBottomSample = (double)(yDst + 1) * nHeightRatio; DoubleRect drcSample = new DoubleRect( xLeftSample, yTopSample, xRightSample, yBottomSample); Color clrSample = SampleBitmap(bmSrc, drcSample); bmDst.SetPixel(xDst, yDst, clrSample); } } }
static unsafe Color SampleTemplateBitmap(Bitmap bm, Bitmap bmMask, DoubleRect drc, double nAreaBackgroundThreshold, double nLuminanceMultBackground, double nSaturationMultBackground, double nLuminanceMultForeground, double nSaturationMultForeground) { // First classify as foreground or background. Calc percentages double nAreaTotal = drc.Width * drc.Height; double nAreaBackground = 0.0; if (bmMask != null) { for (int y = (int)Math.Floor(drc.top); y < (int)Math.Ceiling(drc.bottom); y++) { for (int x = (int)Math.Floor(drc.left); x < (int)Math.Ceiling(drc.right); x++) { // Calc the % of whole taken by this pixel fragment DoubleRect drcPixel = new DoubleRect(x, y, x + 1.0, y + 1.0); drcPixel.Intersect(drc); double nAreaPixel = drcPixel.Width * drcPixel.Height; // Get pixel if (bmMask.GetPixel(x, y) == Color.FromArgb(255, 0, 255)) nAreaBackground += nAreaPixel; } } } // If background is above a threshold, this pixel will be background, // otherwise foreground. bool fBackground; if (nAreaBackground / nAreaTotal >= nAreaBackgroundThreshold) { fBackground = true; nAreaTotal = nAreaBackground; } else { fBackground = false; nAreaTotal -= nAreaBackground; } double r = 0; double g = 0; double b = 0; for (int y = (int)Math.Floor(drc.top); y < (int)Math.Ceiling(drc.bottom); y++) { for (int x = (int)Math.Floor(drc.left); x < (int)Math.Ceiling(drc.right); x++) { // Foreground / background? Color clr = bm.GetPixel(x, y); if (bmMask != null) { if (fBackground) { if (bmMask.GetPixel(x, y) != Color.FromArgb(255, 0, 255)) continue; } else { if (bmMask.GetPixel(x, y) == Color.FromArgb(255, 0, 255)) continue; } } // Calc the % of whole taken by this pixel fragment DoubleRect drcPixel = new DoubleRect(x, y, x + 1.0, y + 1.0); drcPixel.Intersect(drc); double nAreaPixel = drcPixel.Width * drcPixel.Height; double nPercentPixel = nAreaPixel / nAreaTotal; // Add in the color components r += clr.R * nPercentPixel; g += clr.G * nPercentPixel; b += clr.B * nPercentPixel; } } // Tweak luminance & saturation Color clrT = Color.FromArgb((int)r, (int)g, (int)b); double nLuminance = clrT.GetBrightness() * (fBackground ? nLuminanceMultBackground : nLuminanceMultForeground); if (nLuminance > 1.0) nLuminance = 1.0; double nSaturation = clrT.GetSaturation() * (fBackground ? nSaturationMultBackground : nSaturationMultForeground); if (nSaturation > 1.0) nSaturation = 1.0; MyHSLtoRGB(clrT.GetHue(), nSaturation, nLuminance); return Color.FromArgb((int)(rT * 255.0), (int)(gT * 255.0), (int)(bT * 255.0)); }
static Color SampleBitmap(Bitmap bm, DoubleRect drc) { double r = 0.0; double g = 0.0; double b = 0.0; double nAreaTotal = drc.Width * drc.Height; for (int y = (int)Math.Floor(drc.top); y < (int)Math.Ceiling(drc.bottom); y++) { for (int x = (int)Math.Floor(drc.left); x < (int)Math.Ceiling(drc.right); x++) { DoubleRect drcPixel = new DoubleRect(x, y, x + 1, y + 1); drcPixel.Intersect(drc); double nArea = drcPixel.Width * drcPixel.Height; double nPercent = nArea / nAreaTotal; Color clr = bm.GetPixel(x, y); r += (double)clr.R / 255.0 * nPercent; g += (double)clr.G / 255.0 * nPercent; b += (double)clr.B / 255.0 * nPercent; } } return Color.FromArgb((int)(r * 255.0), (int)(g * 255.0), (int)(b * 255.0)); }