public static ColorChecker Not(ColorChecker cc) { return(delegate(Color sample) { return !cc(sample); }); }
/// <summary> /// Checks if a screen area includes a given color /// </summary> /// <param name="screen_area">Rectangle representing coordinates of the screen area</param> /// <param name="checker">Color rules. <see cref="ColorRule"/></param> /// <returns></returns> public bool ScreenAreaIncludesColor(Rectangle screen_area, ColorChecker checker) { using (Bitmap bmp = screen_capture.CaptureScreenArea(screen_area)) { return(bmp.IncludesColor(checker)); } // implicit bmp.Dispose() }
private void Update() { if (_IsGame) { if (!_IsRedPlayerComputer || _CurrentMovingPlayer == ColorChecker.Blue) { /* Отслеживание нажатия на экран */ if (Input.GetMouseButtonDown(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { CheckTouchPosition(hit); } } } else if (_CurrentMovingPlayer == ColorChecker.Red) { foreach (var pointFrom in _Points) { if (pointFrom.checker != null && pointFrom.checker.Color == ColorChecker.Red) { foreach (var pointTo in _Points) { if (pointTo.checker == null || pointTo.checker.IsDead) { var distanceBetweenPoints = (int)pointTo.position - (int)pointFrom.position; if (distanceBetweenPoints == (int)_CurrentMovingPlayer * 9 || distanceBetweenPoints == (int)_CurrentMovingPlayer * 11) { _PointFromMove = pointFrom; _PointFromMove.checker.transform.position = pointTo.transform.position; MoveTo(pointTo); _CurrentMovingPlayer = (_CurrentMovingPlayer == ColorChecker.Blue ? ColorChecker.Red : ColorChecker.Blue); return; } else if (Math.Abs(distanceBetweenPoints) == 18 || Math.Abs(distanceBetweenPoints) == 22) { _PointFromMove = pointFrom; _startMaterialChecker = _PointFromMove.checker.GetComponent <MeshRenderer>().material; EatEnemyChecker(distanceBetweenPoints, pointTo, true); if (_CurrentMovingPlayer != ColorChecker.Red) { return; } } } } } } } } }
private void CheckTouchPosition(RaycastHit hit) { foreach (var point in _Points) { if (hit.transform == point.transform) { if (point.checker != null && !_IsMoveAgain && !point.checker.IsDead && point.checker.Color == _CurrentMovingPlayer) { Debug.Log($"[GameplayControl] [CheckTouchPosition] You touched on your checker!"); if (_PointFromMove != null) { _PointFromMove.checker.GetComponent <MeshRenderer>().material = _startMaterialChecker; } _PointFromMove = point; var renderer = _PointFromMove.checker.GetComponent <MeshRenderer>(); _startMaterialChecker = renderer.material; _PointFromMove.checker.GetComponent <MeshRenderer>().material = _YellowMaterial; return; } if (_PointFromMove != null) { if (hit.transform == point.transform && point.checker == null || hit.transform == point.transform && point.checker.IsDead) { var distanceBetweenPoints = (int)point.position - (int)_PointFromMove.position; if (distanceBetweenPoints == (int)_CurrentMovingPlayer * 9 || distanceBetweenPoints == (int)_CurrentMovingPlayer * 11) { var renderer = _PointFromMove.checker.GetComponent <MeshRenderer>(); _PointFromMove.checker.transform.position = point.transform.position; Locator.UiSwitcher.GameMenu.Show(() => { MoveTo(point); _CurrentMovingPlayer = (_CurrentMovingPlayer == ColorChecker.Blue ? ColorChecker.Red : ColorChecker.Blue); renderer.material = _startMaterialChecker; }, () => { _PointFromMove.checker.transform.localPosition = Vector3.zero; renderer.material = _startMaterialChecker; }); } else if (Math.Abs(distanceBetweenPoints) == 18 || Math.Abs(distanceBetweenPoints) == 22) { EatEnemyChecker(distanceBetweenPoints, point); } } } return; } } }
/// <summary> /// Waits for a certain color pixel to appear in the <paramref name="screen_area"/> /// </summary> /// <param name="screen_area">Rectangle representing coordinates of the screen area</param> /// <param name="checker">Color rules. <see cref="ColorRule"/></param> /// <param name="timeout_ms">How long to wait before giving up, in milliseconds. 0 for unlimited time.</param> /// <returns></returns> public bool WaitForAreaInclude(Rectangle screen_area, ColorChecker checker, int timeout_ms = 0) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); while (!ScreenAreaIncludesColor(screen_area, checker)) { if (timeout_ms != 0 && stopwatch.ElapsedMilliseconds >= timeout_ms) { return(false); } } return(true); }
private void OnTriggerEnter2D(Collider2D collision) { Material collisionMat = collision.GetComponent <Renderer>().sharedMaterial; Material myMaterial = this.GetComponent <Renderer>().sharedMaterial; if (ColorChecker.CheckColor(collisionMat, myMaterial)) { Debug.Log("same mat"); } else { Debug.Log("different mat"); } }
/// <summary> /// Waits for a pixel to turn a certain color /// </summary> /// <param name="coords">Absolute pixel coordinates</param> /// <param name="checker">Color rules. <see cref="ColorRule"/></param> /// <param name="timeout_ms">How long to wait before giving up, in milliseconds. 0 for unlimited time.</param> /// <returns></returns> public bool WaitForPx(Point coords, ColorChecker checker, int timeout_ms = 0) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); while (!checker(screen_capture.GetColorOfPx(coords))) { if (timeout_ms != 0 && stopwatch.ElapsedMilliseconds >= timeout_ms) { return(false); // timed out } Thread.Sleep(50); } return(true); // success }
public FunctionCall ifcolor(string arg_txt) { string[] args = arg_txt.Split(','); int X = Int32.Parse(args[0]); int Y = Int32.Parse(args[1]); ColorChecker color_checker = ColorRule.Parse(args[2]); string file_path = (args.Length > 3 ? args[3] : null); return(delegate() { if (color_checker(screen_capturer.GetColorOfPx(X + script.XRef, Y + script.YRef))) { return ExecFileBreakable(file_path); } return FunctionResult.Continue; }); }
/// <summary> /// Gets the location of a color within a given area of the screen /// </summary> /// <param name="screen_area">Rectangle representing coordinates of the screen area</param> /// <param name="checker">Color rules. <see cref="ColorRule"/></param> /// <returns></returns> public Point LocationOfColorWithinScreenArea(Rectangle screen_area, ColorChecker checker) { using (Bitmap bmp = screen_capture.CaptureScreenArea(screen_area)) { // get location of color relative to start Point Point loc = bmp.LocationOfColor(checker); if (loc.X == -1 && loc.Y == -1) { return(loc); // not found } // adjust location so location refers to the absolute screen coords loc.X += screen_area.X; loc.Y += screen_area.Y; return(loc); } // implicit bmp.Dispose() }
public void When_2_Colors_Match_Position_ExactMatchesCount_Returns_2(string[] userColors, string[] initialColors, int expectedExactMatchesCount, int expectedDifferentPositionsMatchsCount ) { //Arrange var colorChecker = new ColorChecker(); //Act var actualExactMatchesCount = colorChecker.ExactMatchesCount(userColors, initialColors); var actualDifferentPositionMatchesCount = colorChecker.DifferentPositionMatchesCount(userColors, initialColors); //Assert Assert.AreEqual(expectedExactMatchesCount, actualExactMatchesCount); Assert.AreEqual(expectedDifferentPositionsMatchsCount, actualDifferentPositionMatchesCount); }
/// <summary> /// Moves mouse cursor to a certain color /// </summary> /// <param name="screen_area">Rectangle representing coordinates of the screen area</param> /// <param name="move_color">Color to move to</param> /// <param name="timeout_ms">How long to wait before giving up, in milliseconds. 0 for unlimited time.</param> /// <param name="speed">Cursor move speed</param> /// <param name="comparer">A color comparer</param> /// <returns>True if success, false if timed out</returns> public bool MoveToColor(Rectangle screen_area, ColorChecker checker, int timeout_ms = 0, double speed = SpeedDefault) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); Point p = new Point(0, 0); while ((p = ScreenColorDetector.LocationOfColorWithinScreenArea( screen_area, checker)).X == -1) { if (timeout_ms != 0 && stopwatch.ElapsedMilliseconds >= timeout_ms) { return(false); } Thread.Sleep(10); } MoveTo(p.X, p.Y, speed); return(true); }
/// <summary> /// Finds a location of a given color within a Bitmap image. /// </summary> /// <param name="bmp">A Bitmap image</param> /// <param name="checker">Color rules. <see cref="ColorRule"/></param> /// <returns>Returns Point representing coordinates, or (-1, -1) if not found.</returns> public static Point LocationOfColor(this Bitmap bmp, ColorChecker checker) { // TODO: Update this implementation to increment y and x by about y/10 and x/10, respectively. // This will greatly improve the efficiency of the algorithm, since similar colors are normally // grouped together on a computer screen. // Be careful to ensure that all coordinates are checked (do proper testing on this). for (int y = 0; y < bmp.Height; y++) { for (int x = 0; x < bmp.Width; x++) { if (checker(bmp.GetPixel(x, y))) { return(new Point(x, y)); } } } return(new Point(-1, -1)); // color not found }
public FunctionCall waitforpx(string arg_txt) { string[] args = arg_txt.Split(','); int x = Int32.Parse(args[0]); int y = Int32.Parse(args[1]); ColorChecker color_checker = ColorRule.Parse(args[2]); NumberGenerator timeout_ms = (args.Length > 3 ? NumberGenerator.Parse(args[3]) : new StaticNumber(0)); string file_path = (args.Length > 4 ? args[4] : null); return(delegate() { if (!screen_color_detector.WaitForPx(new Point(x + script.XRef, y + script.YRef), color_checker, timeout_ms.GetInt())) { // timed out return ExecFileBreakable(file_path); } return FunctionResult.Continue; }); }
public FunctionCall ifarea(string arg_txt) { string[] args = arg_txt.Split(','); int x1 = Int32.Parse(args[0]); int y1 = Int32.Parse(args[1]); int x2 = Int32.Parse(args[2]); int y2 = Int32.Parse(args[3]); ColorChecker color_checker = ColorRule.Parse(args[4]); string file_path = args.Length > 5 ? args[5] : null; return(delegate() { Rectangle screen_area = Rectangle.FromLTRB(x1 + script.XRef, script.YRef + y1, script.XRef + x2, script.YRef + y2); if (screen_color_detector.ScreenAreaIncludesColor(screen_area, color_checker)) { return ExecFileBreakable(file_path); } return FunctionResult.Continue; }); }
public FunctionCall movetocolor(string arg_txt) { string[] args = arg_txt.Split(','); NumberGenerator x1 = NumberGenerator.Parse(args[0]); NumberGenerator y1 = NumberGenerator.Parse(args[1]); NumberGenerator x2 = NumberGenerator.Parse(args[2]); NumberGenerator y2 = NumberGenerator.Parse(args[3]); ColorChecker color_checker = ColorRule.Parse(args[4]); NumberGenerator timeout = new StaticNumber(0); string file_path = null; if (args.Length > 5) { timeout = NumberGenerator.Parse(args[5]); if (args.Length > 6) { file_path = args[6]; } } return(delegate() { if (!script.MouseMover.MoveToColor( Rectangle.FromLTRB(x1.GetInt() + script.XRef, y1.GetInt() + script.YRef, x2.GetInt() + script.XRef, y2.GetInt() + script.YRef), color_checker, timeout.GetInt(), script.MouseSpeed.GetDouble())) { // timed out return ExecFileBreakable(file_path); } return FunctionResult.Continue; }); }
private void EatEnemyChecker(int distanceBetweenPoints, Point pointTo, bool isYes = false) { var element = GetPoint((BoardPosition)((int)_PointFromMove.position + distanceBetweenPoints / 2)); if (element != null && element.checker != null && element.checker.Color != _PointFromMove.checker.Color) { _PointFromMove.checker.transform.position = pointTo.transform.position; _PointFromMove.checker.GetComponent <MeshRenderer>().material = _YellowMaterial; Locator.UiSwitcher.GameMenu.Show(() => { element.checker.IsDead = true; foreach (var player in _Players) { if (player.Color != _CurrentMovingPlayer) { player.NumOfChackers--; if (player.NumOfChackers == 0) { string name = _Players.FirstOrDefault(t => t.Color == _CurrentMovingPlayer).Name; OnFinish(name); _IsGame = false; } } } MoveTo(pointTo); pointTo.checker.GetComponent <MeshRenderer>().material = _startMaterialChecker; foreach (var p in _Points) { if (p.checker != null && p.checker.Color != _CurrentMovingPlayer) { if (p.position == pointTo.position + 9 && !p.checker.IsDead || p.position == pointTo.position - 9 && !p.checker.IsDead || p.position == pointTo.position + 11 && !p.checker.IsDead || p.position == pointTo.position - 11 && !p.checker.IsDead) { var nextPoint = GetPoint((BoardPosition)(2 * (int)p.position - (int)pointTo.position)); if (nextPoint != null && nextPoint.checker == null) { if (!isYes) { _PointFromMove = pointTo; _IsMoveAgain = true; } return; } } } } _IsMoveAgain = false; _CurrentMovingPlayer = (_CurrentMovingPlayer == ColorChecker.Blue ? ColorChecker.Red : ColorChecker.Blue); }, () => { _PointFromMove.checker.transform.localPosition = Vector3.zero; _PointFromMove.checker.GetComponent <MeshRenderer>().material = _startMaterialChecker; }); if (isYes) { Locator.UiSwitcher.GameMenu.ButtonYes(); } } }
/// <summary> /// Extension for Bitmap class /// </summary> /// <param name="bmp">A Bitmap image</param> /// <param name="checker">Color rules. <see cref="ColorRule"/></param> /// <returns></returns> public static bool IncludesColor(this Bitmap bmp, ColorChecker checker) { Point loc = bmp.LocationOfColor(checker); return((loc.X != -1) && (loc.Y != -1)); }
/// <summary> /// Color Correction Model. /// </summary> /// <param name="src">Detected colors of ColorChecker patches; the color type is RGB not BGR, and the color values are in [0, 1];</param> /// <param name="color">The Built-in color card</param> public ColorCorrectionModel(Mat src, ColorChecker color) { _ptr = CcmInvoke.cveColorCorrectionModelCreate(src, color); _needDispose = true; }
public Player(string name, ColorChecker color) { Name = name; Color = color; }