private void ColorChecker(Button a, Button b, Button c, Button d, RichTextBox e) { int colormatch = 0; int positionmatch = 0; a.Enabled = false; b.Enabled = false; c.Enabled = false; d.Enabled = false; Color[] solution = new Color[] { square1, square2, square3, square4 }; if (solution.Contains(a.BackColor)) { colormatch++; } if (solution.Contains(b.BackColor)) { colormatch++; } if (solution.Contains(c.BackColor)) { colormatch++; } if (solution.Contains(d.BackColor)) { colormatch++; } if (square1 == a.BackColor) { positionmatch++; } if (square2 == b.BackColor) { positionmatch++; } if (square3 == c.BackColor) { positionmatch++; } if (square4 == d.BackColor) { positionmatch++; } e.Text = "Number of color matches=" + colormatch + "\n" + "Number of Positon matches" + positionmatch; }
private void button1_Click(object sender, EventArgs e) { // listbox1.Clear(); listbox1.Items.Clear(); Find(richtext, textBox1.Text, x, y); startIndex = 0; clicked = true; var lines = richtext.Lines; int count = 0; List <string> items = new List <string>(); foreach (var x in lines) { count++; if (x.Contains(textBox1.Text)) { SearchedItems s = new SearchedItems(); s.LineNumber = count; s.LineText = x; s.findForm = true; // Items.Add(s); listbox1.Items.Add(s); //listbox1.AppendText(Items.ToString()); //richtext2.AppendText("Line" + s.LineNumber + ": "); Console.WriteLine(x + "\n"); // richtext2.AppendText(s.LineText + "\n"); listbox1.Visible = true; items.Add(x); } } Select(listbox1, textBox1.Text, Color.Yellow, FontStyle.Bold); }
public static Color[] GetBitmapPalette(ref Bitmap sprite) { var source = new Color[16]; byte index = 0; var num4 = sprite.Width - 1; var i = 0; while (i <= num4) { var num5 = sprite.Height - 1; var j = 0; while (j <= num5) { var pixel = sprite.GetPixel(i, j); if (!source.Contains(pixel)) { source[index] = pixel; index = (byte)(index + 1); } if (index > 15) { return(source); } j += 1; } i += 1; } return(source); }
public bool Seacrh(string str) { //TODO не работает if (Name.Contains(str)) { return(true); } if (Type.Contains(str)) { return(true); } if (Category.Contains(str)) { return(true); } if (Manufacturer.Contains(str)) { return(true); } if (Color.Contains(str)) { return(true); } if (Composition.Contains(str)) { return(true); } if (Description.Contains(str)) { return(true); } return(false); }
public static byte GetNumColors(ref Bitmap sprite) { var source = new Color[16]; byte index = 0; var num5 = sprite.Width - 1; var i = 0; while (i <= num5) { var num6 = sprite.Height - 1; var j = 0; while (j <= num6) { var pixel = sprite.GetPixel(i, j); if (!source.Contains(pixel)) { source[index] = pixel; index = (byte)(index + 1); } if (index > 15) { return(0x10); } j += 1; } i += 1; } return(index); }
IEnumerable <ValidationResult> IValidatableObject.Validate(ValidationContext validationContext) { // Blue buses are not allowed if (Color.Contains("blue", StringComparison.CurrentCultureIgnoreCase)) { if (Id == 0) { if (VehicleType.Name.Contains("bus", StringComparison.CurrentCultureIgnoreCase)) { yield return(new ValidationResult("Blue buses are not allowed", new[] { "Color" })); } } else { var context = (Garage25Context)validationContext.GetService(typeof(Garage25Context)); VehicleType vehicleType = context.VehicleType.First(v => v.Id == VehicleTypeId); if (vehicleType.Name.Contains("bus", StringComparison.CurrentCultureIgnoreCase)) { yield return(new ValidationResult("Blue buses are not allowed", new[] { "Color" })); } } } else { yield return(ValidationResult.Success); } }
private Color[] CreateCode() { if (_isDuplicates) { Color[] code = new Color[4]; for (int i = 0; i < 4; i++) { int index = rnd.Next(0, 6); code[i] = _allColors[index]; } return(code); } else { Color[] code = new Color[4]; for (int i = 0; i < 4; i++) { bool x = true; while (x) { int index = rnd.Next(0, 6); if (!code.Contains(_allColors[index])) { x = false; code[i] = _allColors[index]; } } } return(code); } }
public bool Search(string query) { return (query is null || Name.Contains(query, StringComparison.OrdinalIgnoreCase) || Vendor.Name.Contains(query, StringComparison.OrdinalIgnoreCase) || Color.Contains(query, StringComparison.OrdinalIgnoreCase) || Categories.FirstOrDefault(c => c.Name.Contains(query, StringComparison.OrdinalIgnoreCase)) != null); }
private Color[] GetColorsArray(int size) { List <Color> allColors = GetAllColors(); int listCount = allColors.Count; Color[] colors = new Color[size]; var currentColor = allColors[rnd.Next(0, listCount)]; for (int i = 0; i < colors.Length; i++) { currentColor = allColors[rnd.Next(0, listCount)]; while (colors.Contains(currentColor) && i < listCount - 1) { currentColor = allColors[rnd.Next(0, listCount)]; } colors[i] = currentColor; } return(colors); }
public static Color Combine(this Color color, Color otherColor) { // primary combinations if ((color == Red && otherColor == Blue) || (color == Blue && otherColor == Red)) return Purple; if ((color == Blue && otherColor == Yellow) || (color == Yellow && otherColor == Blue)) return Green; if ((color == Red && otherColor == Yellow) || (color == Yellow && otherColor == Red)) return Orange; // redundant color combinations if (color.Contains(otherColor)) return color; if (otherColor.Contains(color)) return otherColor; #if DEBUG Console.WriteLine(String.Format("Possibly unsupported color combination: {0} and {1}", color.ToString(), otherColor.ToString())); #endif return Black; }
private void BtnGenerate_Click(object sender, EventArgs e) { Color[] used_colors = new Color[12]; pnlResult.Refresh(); int i = 0, yPos = 0; txtResult.Text = ""; while (i < amount) { int picked = rn.Next(0, colors.Length); Color color = colors[picked]; if (!used_colors.Contains(color)) { g.FillRectangle(new SolidBrush(color), 0, yPos, 140, 370 / amount); used_colors[i] = color; yPos += 370 / amount; i++; txtResult.Text += $"{i}:\t{color.Name}" + Environment.NewLine; } } }
private void CreateDrawLines(string type, bool isCasing) { AppendLine("draw:"); _indent++; if (Color != null) { int colorStringLength = 0; bool forceBlended = LayerUtils.IsBlendedLayer(LayerWithSource.Id); if (!forceBlended) { if (!Color.Contains(",")) { colorStringLength = Color.Length; } else { // todo split this kind of thing to get the length of the color strings inside. "[[5,'#F2934A'],[6,'#fc8']]" } } if (forceBlended || colorStringLength > 7) // i.e. longer than #FFFFFF so it has an A component. { if (type == "polygons") { type = "polys-blended"; } else if (type == "lines") { type = "lines-blended"; } } } AppendLine(type + ":"); _indent++; if (type != TangramText) { AppendLine("order: " + LayerUtils.GetLayerOrder(LayerWithSource, isCasing || Outline != null)); } else { AppendLine("priority: " + LayerUtils.GetTextLayerPriority(LayerWithSource)); } //if (LayerWithSource.Id.StartsWith("road")) // AppendLine("collide: false"); if (Outline != null) { AppendLine("outline:"); _indent++; if (Outline.Width != null) { AppendLine("width: " + Outline.Width); } if (Outline.Color != null) { AppendLine("color: " + Outline.Color); } _indent--; } if (Color != null) { AppendLine("color: " + Color); } //AppendLine("color: " + (!isCasing ? Color : "'#FF0000'")); if (Width != null) { AppendLine("width: " + Width); } if (!isCasing) { if (Join != null) { AppendLine("join: " + Join); } if (Cap != null && !LayerWithSource.Id.StartsWith("bridge")) { AppendLine("cap: " + Cap); } } if (Font != null && type != TangramIcons) { AppendFont(); } if (Sprite != null && !Sprite.Contains("motorway-exit")) { AppendLine("sprite: " + Sprite); //AppendLine("blend_order: 1"); //AppendLine("priority: 21"); //AppendLine("order: 100"); //AppendLine("size: 24px"); bool preciseIcon = Sprite.Contains("dot"); AppendLine("collide: false"); if (!preciseIcon) { AppendLine("anchor: top"); } _indent--; if (type == TangramIcons && Sprite != null) { DrawIconText(preciseIcon); } } else if (type == TangramIcons && Font != null) { _indent--; DrawIconText(false); _indent++; } _indent--; }
private void Feedback(int col, Color[] _currentCode) { Ellipse ellipse1 = _ellipses[0, col]; Color color1 = ((SolidColorBrush)ellipse1.Fill).Color; Ellipse ellipse2 = _ellipses[1, col]; Color color2 = ((SolidColorBrush)ellipse2.Fill).Color; Ellipse ellipse3 = _ellipses[2, col]; Color color3 = ((SolidColorBrush)ellipse3.Fill).Color; Ellipse ellipse4 = _ellipses[3, col]; Color color4 = ((SolidColorBrush)ellipse4.Fill).Color; if (color1 != _selectionButtonGray && color2 != _selectionButtonGray && color3 != _selectionButtonGray && color4 != _selectionButtonGray) { _tries += 1; _moveOnToNextGuess = true; Color[] attempt = new Color[4] { color1, color2, color3, color4 }; Color[] duplicateAttempt = new Color[4]; attempt.CopyTo(duplicateAttempt, 0); Color[] duplicateCurrentCode = new Color[4]; _currentCode.CopyTo(duplicateCurrentCode, 0); int rightColorAndPlace = 0; int rightColorButWrongPlace = 0; for (int i = 0; i < 4; i++) { if (duplicateAttempt[i] == duplicateCurrentCode[i]) { rightColorAndPlace += 1; duplicateCurrentCode[i] = Colors.White; duplicateAttempt[i] = Colors.White; } } for (int i = 0; i < 4; i++) { if (duplicateAttempt[i] != Colors.White) { if (duplicateCurrentCode.Contains(duplicateAttempt[i])) { int codeIndex = Array.IndexOf(duplicateCurrentCode, duplicateAttempt[i]); duplicateAttempt[i] = Colors.White; duplicateCurrentCode[codeIndex] = Colors.White; rightColorButWrongPlace += 1; } } } TextBlock textbox1 = _textboxes1[_tries - 1]; TextBlock textbox2 = _textboxes2[_tries - 1]; textbox1.VerticalAlignment = VerticalAlignment.Center; textbox2.VerticalAlignment = VerticalAlignment.Center; string rightColorWrongPlaceString = ""; string rightColorPlaceString = ""; for (int i = 0; i < rightColorButWrongPlace; i++) { rightColorWrongPlaceString += "\uE734 "; } for (int i = 0; i < rightColorAndPlace; i++) { rightColorPlaceString += "\uE735 "; } textbox1.Text = rightColorPlaceString; textbox2.Text = rightColorWrongPlaceString; if (rightColorAndPlace == 4) { _isSolved = true; EndScreen(true); Ellipse e1 = _ellipses[0, _maxAttempts]; SolidColorBrush CodePeg1 = new SolidColorBrush(_currentCode[0]); e1.Fill = CodePeg1; Ellipse e2 = _ellipses[1, _maxAttempts]; SolidColorBrush CodePeg2 = new SolidColorBrush(_currentCode[1]); e2.Fill = CodePeg2; Ellipse e3 = _ellipses[2, _maxAttempts]; SolidColorBrush CodePeg3 = new SolidColorBrush(_currentCode[2]); e3.Fill = CodePeg3; Ellipse e4 = _ellipses[3, _maxAttempts]; SolidColorBrush CodePeg4 = new SolidColorBrush(_currentCode[3]); e4.Fill = CodePeg4; _isSolutionVisibleOnBoard = true; } else if (_tries == _maxAttempts) { _isSolved = true; Ellipse e1 = _ellipses[0, _maxAttempts]; SolidColorBrush CodePeg1 = new SolidColorBrush(_currentCode[0]); e1.Fill = CodePeg1; Ellipse e2 = _ellipses[1, _maxAttempts]; SolidColorBrush CodePeg2 = new SolidColorBrush(_currentCode[1]); e2.Fill = CodePeg2; Ellipse e3 = _ellipses[2, _maxAttempts]; SolidColorBrush CodePeg3 = new SolidColorBrush(_currentCode[2]); e3.Fill = CodePeg3; Ellipse e4 = _ellipses[3, _maxAttempts]; SolidColorBrush CodePeg4 = new SolidColorBrush(_currentCode[3]); e4.Fill = CodePeg4; EndScreen(false); } } }
//********************************************************************* //Method: private static void Draw(int[] aGrades) //Purpose: Draw The amount of Grades in ranges of 10 up to 100 as bars //Parameters: int[] aGrades - Arrays of Grades //Returns: Nothing //********************************************************************* private static void DrawHistogram(int[] aGrades) { int[] numRange = new int[11]; //Store amount of numbers in a each range int index; //Index of an array CDrawer Draw = new CDrawer(); //Window to Draw the histogram Color[] cGrade = new Color[11]; //Color of the bar of the histrogram Color cStore; //Temporay store colors Random rColor = new Random(); //Rng use to generate colors string[] stringRange = new string[11]; //Ranges in string form to use as labels double height; //Height of an bar double width; //Width of an bar //Count amount of numbers in each Range for (index = 0; index < aGrades.Length; index++) { //Check to see if the number is below or above 50 if (aGrades[index] < 50) { //Check to see what range the number is in if ((0 <= aGrades[index]) && (aGrades[index] <= 9)) { //Increase the amount in the range 0-9 by 1 numRange[0]++; } else if (aGrades[index] <= 19) { //Increase the amount in the range of 10-19 by 1 numRange[1]++; } else if (aGrades[index] <= 29) { //Increase the amount in the range 20-29 by 1 numRange[2]++; } else if (aGrades[index] <= 39) { //Increase the amount in the range 30-39 by 1 numRange[3]++; } else { //Increase the amount in the range 30-39 by 1 numRange[4]++; } } else { //Check to see what range the number is in if ((50 <= aGrades[index]) && (aGrades[index] <= 59)) { //Increase the amount in the range 50-59 by 1 numRange[5]++; } else if (aGrades[index] <= 69) { //Increase the amount in the range 60-69 by 1 numRange[6]++; } else if (aGrades[index] <= 79) { //Increase the amount in the range 70-79 by 1 numRange[7]++; } else if (aGrades[index] <= 89) { //Increase the amount in the range 80-89 by 1 numRange[8]++; } else if (aGrades[index] <= 99) { //Increase the amount in the range 80-89 by 1 numRange[9]++; } else { //Increase the amouint that eqaul 100 by 1 numRange[10]++; } } } //Add colors and strings to the Arrays for (index = 0; index < cGrade.Length; index++) { //Gerate random colors and make sure they are not already in the array do { cStore = Color.FromArgb(rColor.Next(0, 255), rColor.Next(0, 255), rColor.Next(0, 255)); }while (cGrade.Contains(cStore)); //Add the color to the Array cGrade[index] = cStore; //Add the strings to the Array depending on the Index if (index == 0) { //If it the first Index store 0 to 9 in the Array stringRange[index] = $"0 to 9"; } else if (index == cGrade.Length - 1) { //If it the Last index store the index number follow by a zero in the Array stringRange[index] = $"{index}0"; } else { //Any other Index store {index}0 to {index}9 to the Array. stringRange[index] = $"{index}0 to {index}9"; } } //Draw the Histogram for (index = 0; index < numRange.Length; index++) { //Calculate the width of an bar width = 800 / numRange.Length; //Draw the bar of the Histogram if (numRange[index] != 0) { //Calculate the Length of an bar height = numRange[index]; height /= numRange.Max(); height *= 580; //Draw the bars and the amount in the Bars Draw.AddRectangle((Convert.ToInt32(width * index)), 580 - Convert.ToInt32(height), Convert.ToInt32(width), Convert.ToInt32(height), cGrade[index], 0); Draw.AddText($"{numRange[index]}", 20, Convert.ToInt32(width * index), Convert.ToInt32((height / 2) + 564 - Convert.ToInt32(height)), Convert.ToInt32(width), 32, Color.Black); } //draw the labels of the Historgram Draw.AddText($"{stringRange[index]}", 12, Convert.ToInt32(width * index), 580, Convert.ToInt32(width), 20, Color.White); } }
public void Generate() { // Highly experimental // Componer paleta Color[] tpalette = new Color[paleta_extra.Length];//[256]; int i = 0; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { Color color = bitmap.GetPixel(x, y); if (!tpalette.Contains(color)) { tpalette.SetValue(color, i); i++; } } } // Pasar la paleta a 555Color palette = new _555Color[tpalette.Length]; for (int j = 0; j < tpalette.Length; j++) { //palette.SetValue(new _555Color(tpalette[j]), j); palette.SetValue(paleta_extra[j], j); } // Generar los tiles numericos int total = width * height; total_tiles = (ushort)(total / 64); int current = 0; int extra_x = 0; int extra_y = 0; // array con todos los bytes de los tiles tiles = new byte[width * height]; while (current < total) { for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x++) { Color color = bitmap.GetPixel(x + extra_x, y + extra_y); _555Color color2 = new _555Color(color); for (int j = 0; j < palette.Length; j++) { if (palette[j].Int16 == color2.Int16) { // Guardar la posicion del color tiles.SetValue((byte)j, current); break; } else { tiles.SetValue((byte)0, current); } } current++; } } extra_x += 8; if (extra_x >= width) { extra_x = 0; extra_y += 8; } } }
public override void Process() { var projectType = File.Project.Type; var bitDepth = BitDepth; var tileSize = 8 * bitDepth; var paletteEntries = (int)Math.Pow(2, bitDepth); using (var image = GetImageSource()) { var tileCount = (image.Width / 8) * (image.Height / 8); var bytes = new byte[tileCount * tileSize]; var exportedTiles = new List <byte[]>(); var palette = new Color[paletteEntries]; var knownEntries = 0; foreach (var assignment in PaletteAssignment) { if (assignment.Value >= paletteEntries) { continue; } palette[assignment.Value] = assignment.Key; knownEntries = Math.Max(knownEntries, assignment.Value + 1); } if (image.Palette.Entries.Length > knownEntries) { var j = 0; for (var i = knownEntries; i < paletteEntries; i++) { palette[i] = image.Palette.Entries[j]; knownEntries++; j++; } } for (var y = 0; y < image.Height && knownEntries < paletteEntries; y++) { for (var x = 0; x < image.Width && knownEntries < paletteEntries; x++) { var color = image.GetPixel(x, y); if (!palette.Contains(color)) { palette[knownEntries] = color; knownEntries++; } } } if (knownEntries > paletteEntries) { throw new Exception("Too many colors in image"); } var tileByteOffset = 0; for (var i = 0; i < tileCount; i++) { var tileData = new byte[tileSize]; var offset = i * 8; var offsetY = (offset / image.Width) * 8; var offsetX = (offset % image.Width); for (var y = 0; y < 8; y++) { for (var x = 0; x < 8; x++) { var color = image.GetPixel(offsetX + x, offsetY + y); var colorIndex = -1; for (var c = 0; c < knownEntries; c++) { if (color == palette[c]) { colorIndex = c; break; } } if (colorIndex == -1) { if (knownEntries >= paletteEntries) { throw new Exception("Too many colors in image"); } palette[knownEntries] = color; colorIndex = knownEntries; knownEntries++; } for (var j = 0; j <= (bitDepth / 2); j += 2) { var byte0 = (colorIndex & (1 << j)) == 0 ? 0 : 1; var byte1 = (colorIndex & (1 << (j + 1))) == 0 ? 0 : 1; var mask0 = (byte)(byte0 << (7 - x)); var mask1 = (byte)(byte1 << (7 - x)); var byte0Index = (8 * j) + (projectType == ProjectType.Snes ? y * 2 : y); var byte1Index = (8 * j) + (projectType == ProjectType.Snes ? y * 2 + 1 : y + 8); tileData[byte0Index] |= mask0; tileData[byte1Index] |= mask1; } } } if (DiscardRedundantTiles && exportedTiles.Any(t => t.SequenceEqual(tileData))) { continue; } exportedTiles.Add(tileData); tileData.CopyTo(bytes, tileByteOffset); tileByteOffset += tileSize; } using (var outputFile = System.IO.File.Create(ChrOutput)) { outputFile.Write(bytes, 0, tileByteOffset); outputFile.Close(); } if (ExportPalette) { var paletteData = new byte[knownEntries * 2]; for (var i = 0; i < knownEntries; i++) { var color = GetColorValue(palette[i]); paletteData[i * 2] = (byte)(color & 0xFF); paletteData[i * 2 + 1] = (byte)((color >> 8) & 0xFF); } using (var outputFile = System.IO.File.Create(PaletteOutput)) { outputFile.Write(paletteData, 0, paletteData.Length); outputFile.Close(); } } } LastProcessed = DateTime.Now; }
private void button1_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { bmp = (Bitmap)Bitmap.FromFile(openFileDialog1.FileName); pictureBox1.Height = bmp.Height; pictureBox1.Width = bmp.Width; pictureBox1.Image = bmp; if (pictureBox1.Height > 240) { this.Height = pictureBox1.Top + pictureBox1.Height + 60; } this.Width = pictureBox1.Left + pictureBox1.Width + 40; for (int row = 0; row < bmp.Height; row++) { int coloursThisRow = 0; Color[] rowColors = new Color[0]; for (int stitch = 0; stitch < bmp.Width; stitch++) { thisStitch = bmp.GetPixel(stitch, row); if (!colors.Contains(thisStitch)) { Array.Resize(ref colors, colors.Length + 1); colors[colors.Length - 1] = thisStitch; } if (!rowColors.Contains(thisStitch)) { Array.Resize(ref rowColors, rowColors.Length + 1); rowColors[rowColors.Length - 1] = thisStitch; coloursThisRow++; } if (coloursThisRow > MaxColoursPerRow) { MaxColoursPerRow = coloursThisRow; } } } for (int i = 0; i < colors.Length; i++) { Array.Resize(ref colorButtons, colorButtons.Length + 1); colorButtons[colorButtons.Length - 1] = new Button(); colorButtons[colorButtons.Length - 1].Left = 10; colorButtons[colorButtons.Length - 1].Top = i * (colorButtons[colorButtons.Length - 1].Height + 3) + 60; colorButtons[colorButtons.Length - 1].BackColor = colors[i]; this.Controls.Add(colorButtons[colorButtons.Length - 1]); Array.Resize(ref colorNumber, colorNumber.Length + 1); colorNumber[colorNumber.Length - 1] = new TextBox(); colorNumber[colorNumber.Length - 1].Left = colorButtons[colorButtons.Length - 1].Left + colorButtons[colorButtons.Length - 1].Width + 10; colorNumber[colorNumber.Length - 1].Top = colorButtons[colorButtons.Length - 1].Top; colorNumber[colorNumber.Length - 1].Text = (i + 1).ToString(); this.Controls.Add(colorNumber[colorNumber.Length - 1]); } label1.Text = "Max colours per row: " + MaxColoursPerRow.ToString(); label2.Text = "Rows: " + bmp.Height.ToString(); label3.Text = "Stitches: " + bmp.Width.ToString(); // button1.Text = colors.Length.ToString(); if (MaxColoursPerRow > 2) { buttonConvert.Visible = true; button1.Visible = false; } else { buttonConvert.Visible = false; button1.Visible = true; } } }
private void button2_Click(object sender, EventArgs e) { int colour; int i; output = new Byte[bmp.Height, bmp.Width]; for (int row = 0; row < bmp.Height; row++) { Color[] rowColors = new Color[MaxColoursPerRow]; int colorCount = 0; for (int stitch = 0; stitch < bmp.Width; stitch++) { thisStitch = bmp.GetPixel(stitch, row); if (!rowColors.Contains(thisStitch)) { rowColors[colorCount++] = thisStitch; } } for (int stitch = 0; stitch < bmp.Width; stitch++) { thisStitch = bmp.GetPixel(stitch, row); for (i = 0; i < colors.Length; i++) { if (thisStitch == colors[i]) { colour = Convert.ToInt32(colorNumber[i].Text); output[row, stitch] = (Byte)colour; break; } } } } // Array 'output' now contains one byte per pixel (stitch), with number representing colour of yarn to use. Let's dump it to a file for reference. System.IO.StreamWriter stream = new System.IO.StreamWriter("C:\\Knitulator\\Frontproof.txt"); for (int row = 0; row < bmp.Height; row++) { for (int stitch = 0; stitch < bmp.Width; stitch++) { stream.Write(output[row, stitch].ToString()); } stream.WriteLine(); } stream.Close(); // Now lets create a Multicolour pattern, where each row contains a boolean indicating do or don't use the colour. // A separate array is required, for each row to indicate which colour to use. //TOD: Make sure patternRowColour is multiple of 2 as colours are stored as nibbles Byte[] patternRowColour = new Byte[bmp.Height * MaxColoursPerRow]; // Array to hold colour of yarn to use on each Multicolour row int widthInBits = (int)(8 * Math.Round(bmp.Width / (double)8, MidpointRounding.AwayFromZero)); // must be multiple of 8 bits Byte[,] pattern = new Byte[bmp.Height * MaxColoursPerRow, widthInBits]; // Array to hold pattern data = 1 byte represents 8 stitches System.IO.StreamWriter stream2 = new System.IO.StreamWriter("C:\\Knitulator\\Frontmc.txt"); int n = bmp.Height * MaxColoursPerRow; stream2.WriteLine("Row : Disp : Col : Row Pattern"); for (int row = 0; row < bmp.Height; row++) { // Work out which colours to knit: Byte[] rowColours = new Byte[MaxColoursPerRow]; int colourCount = 0; for (int stitch = 0; stitch < bmp.Width; stitch++) { if (!rowColours.Contains(output[row, stitch])) { rowColours[colourCount++] = output[row, stitch]; } } if (rowColours[1] == 0) { rowColours[1] = 8; rowColours[2] = 9; } if (rowColours[2] == 0) { rowColours[2] = 9; } for (i = 0; i < MaxColoursPerRow; i++) { stream2.Write((bmp.Height - row).ToString("D3") + " : "); stream2.Write(n.ToString("D3") + " : "); n--; stream2.Write(rowColours[i].ToString() + " : "); patternRowColour[row * MaxColoursPerRow + i] = rowColours[i]; for (int stitch = 0; stitch < bmp.Width; stitch++) { if (output[row, stitch] == rowColours[i]) { stream2.Write("X"); pattern[MaxColoursPerRow * row + i, stitch] = 1; } else { stream2.Write("."); pattern[MaxColoursPerRow * row + i, stitch] = 0; } } stream2.WriteLine(); } } stream2.Close(); // Now create binary array in brother format, 1 bit per pixel Byte[,] patternOut = new Byte[bmp.Height * MaxColoursPerRow, widthInBits / 8]; for (int row = 0; row < bmp.Height * MaxColoursPerRow; row++) { Byte bit = 0; for (int stitch = 0; stitch < widthInBits; stitch++) { bit = (Byte)(stitch & 0x07); bit = (Byte)(0x80 >> bit); if (pattern[row, stitch] != 0) { patternOut[row, stitch / 8] = (Byte)(patternOut[row, stitch / 8] | ( bit) ); } // patternOut[row, stitch / 8] = (Byte)(patternOut[row, stitch / 8] | ((pattern[row, stitch] == 0) ? 0 : (2 ^ bit) )); } } System.IO.FileStream fs = new System.IO.FileStream("C:\\Knitulator\\track-01.dat", System.IO.FileMode.OpenOrCreate); System.IO.BinaryWriter binaryWriter = new System.IO.BinaryWriter(fs); binaryWriter.Seek(0, System.IO.SeekOrigin.Begin); binaryWriter.Write((Byte)0x01); binaryWriter.Write((Byte)0x20); Byte heightHundreds = (Byte)((bmp.Height * MaxColoursPerRow) / 100); Byte heightTens = (Byte)(((bmp.Height * MaxColoursPerRow) / 10) - (10 * heightHundreds)); Byte heightUnits = (Byte)((bmp.Height * MaxColoursPerRow) - (10 * heightTens) - (100 * heightHundreds)); Byte widthHundreds = (Byte)((widthInBits) / 100); Byte widthTens = (Byte)(((widthInBits) / 10) - (10 * widthHundreds)); Byte widthUnits = (Byte)((widthInBits) - (10 * widthTens) - (100 * widthHundreds)); //binaryWriter.Write((Byte)0x77);//774 high //binaryWriter.Write((Byte)0x41); //binaryWriter.Write((Byte)0x74);// 174 wide binaryWriter.Write((Byte)((heightHundreds << 4) + heightTens)); binaryWriter.Write((Byte)((heightUnits << 4) + widthHundreds)); binaryWriter.Write((Byte)((widthTens << 4) + widthUnits)); binaryWriter.Write((Byte)0x89);// 89 binaryWriter.Write((Byte)0x01);//01 = pattern mode 8 number 901 binaryWriter.Write((Byte)0x00); binaryWriter.Write((Byte)0x00); binaryWriter.Write((Byte)0x00); binaryWriter.Write((Byte)0x00); binaryWriter.Write((Byte)0x00); binaryWriter.Write((Byte)0x09); binaryWriter.Write((Byte)0x02); //fs.Position = 0x7ee0 - (patternRowColour.Length / 2) - (bmp.Height * MaxColoursPerRow * widthInBits / 8); //binaryWriter = new System.IO.BinaryWriter(fs); binaryWriter.Seek(0x7ee0 - (patternRowColour.Length / 2) - (bmp.Height * MaxColoursPerRow * widthInBits / 8), System.IO.SeekOrigin.Begin); for (int row = 0; row < bmp.Height * MaxColoursPerRow; row++) { for (int stitchBlock = 0; stitchBlock < widthInBits / 8; stitchBlock++) { binaryWriter.Write(patternOut[row, stitchBlock]); Console.Write(patternOut[row, stitchBlock].ToString("X2") + " "); } Console.WriteLine(); } //binaryWriter.Close(); //fs.Close(); //System.IO.FileStream fs2 = new System.IO.FileStream("C:\\Users\\kevin.blain\\Documents\\track-01.dat", System.IO.FileMode.OpenOrCreate); //fs2.Position = 0x7ee0 - (patternRowColour.Length/2); binaryWriter = new System.IO.BinaryWriter(fs); for (int row = 0; row < bmp.Height * MaxColoursPerRow; row += 2) { Byte colourInfo = 0; if (patternRowColour[row] == 0) { colourInfo = 1 << 4; } else { colourInfo = (Byte)(patternRowColour[row] << 4); } if (patternRowColour[row + 1] == 0) { colourInfo += 1; } else { colourInfo += (Byte)(patternRowColour[row + 1]); } binaryWriter.Write(colourInfo); } binaryWriter.Close(); fs.Close(); MessageBox.Show("Conversion Complete."); }
private void button1_Click_1(object sender, EventArgs e) { int colour; int i; output = new Byte[bmp.Height, bmp.Width]; for (int row = 0; row < bmp.Height; row++) { Color[] rowColors = new Color[MaxColoursPerRow]; int colorCount = 0; for (int stitch = 0; stitch < bmp.Width; stitch++) { thisStitch = bmp.GetPixel(stitch, row); if (!rowColors.Contains(thisStitch)) { rowColors[colorCount++] = thisStitch; } } for (int stitch = 0; stitch < bmp.Width; stitch++) { thisStitch = bmp.GetPixel(stitch, row); for (i = 0; i < colors.Length; i++) { if (thisStitch == colors[i]) { colour = Convert.ToInt32(colorNumber[i].Text); output[row, stitch] = (Byte)colour; break; } } } } // Array 'output' now contains one byte per pixel (stitch), with number representing colour of yarn to use. Let's dump it to a file for reference. System.IO.StreamWriter stream = new System.IO.StreamWriter("C:\\Knitulator\\proof.txt"); for (int row = 0; row < bmp.Height; row++) { for (int stitch = 0; stitch < bmp.Width; stitch++) { stream.Write(output[row, stitch].ToString()); } stream.WriteLine(); } stream.Close(); // Now lets create a 2 colour pattern, where each row contains a boolean indicating do or don't use the contrast yarn. int widthInBits = (int)(8 * Math.Round(bmp.Width / (double)8, MidpointRounding.AwayFromZero)); // must be multiple of 8 bits Byte[,] pattern = new Byte[bmp.Height, widthInBits]; // Array to hold pattern data = 1 byte represents 8 stitches System.IO.StreamWriter stream2 = new System.IO.StreamWriter("C:\\Knitulator\\2col.txt"); int n = bmp.Height; stream2.WriteLine("Row : Row Pattern"); for (int row = 0; row < bmp.Height; row++) { stream2.Write((bmp.Height - row).ToString("D3") + " : "); n--; for (int stitch = 0; stitch < bmp.Width; stitch++) { if (output[row, stitch] == 2) { stream2.Write("X"); pattern[row, stitch] = 1; } else { stream2.Write("."); pattern[row, stitch] = 0; } } stream2.WriteLine(); } stream2.Close(); // Now create binary array in brother format, 1 bit per pixel Byte[,] patternOut = new Byte[bmp.Height, widthInBits / 8]; for (int row = 0; row < bmp.Height; row++) { Byte bit = 0; for (int stitch = 0; stitch < widthInBits; stitch++) { bit = (Byte)(stitch & 0x07); bit = (Byte)(0x80 >> bit); patternOut[row, stitch / 8] = (Byte)(patternOut[row, stitch / 8] | ((2 ^ bit) * pattern[row, stitch])); } } System.IO.FileStream fs = new System.IO.FileStream("C:\\Knitulator\\track-01.dat", System.IO.FileMode.OpenOrCreate); System.IO.BinaryWriter binaryWriter = new System.IO.BinaryWriter(fs); binaryWriter.Seek(0, System.IO.SeekOrigin.Begin); binaryWriter.Write((Byte)0x01); binaryWriter.Write((Byte)0x20); Byte heightHundreds = (Byte)((bmp.Height) / 100); Byte heightTens = (Byte)(((bmp.Height) / 10) - (10 * heightHundreds)); Byte heightUnits = (Byte)((bmp.Height) - (10 * heightTens) - (100 * heightHundreds)); Byte widthHundreds = (Byte)((widthInBits) / 100); Byte widthTens = (Byte)(((widthInBits) / 10) - (10 * widthHundreds)); Byte widthUnits = (Byte)((widthInBits) - (10 * widthTens) - (100 * widthHundreds)); binaryWriter.Write((Byte)((heightHundreds << 4) + heightTens)); binaryWriter.Write((Byte)((heightUnits << 4) + widthHundreds)); binaryWriter.Write((Byte)((widthTens << 4) + widthUnits)); binaryWriter.Write((Byte)0x49);// 49 binaryWriter.Write((Byte)0x01);//01 = pattern mode 8 number 901 binaryWriter.Write((Byte)0x00); binaryWriter.Write((Byte)0x00); binaryWriter.Write((Byte)0x00); binaryWriter.Write((Byte)0x00); binaryWriter.Write((Byte)0x00); binaryWriter.Write((Byte)0x09); binaryWriter.Write((Byte)0x02); binaryWriter.Seek(0x7ee0 - (bmp.Height * widthInBits / 8), System.IO.SeekOrigin.Begin); for (int row = 0; row < bmp.Height ; row++) { for (int stitchBlock = 0; stitchBlock < widthInBits / 8; stitchBlock++) { binaryWriter.Write(patternOut[row, stitchBlock]); Console.Write(patternOut[row, stitchBlock].ToString("X2") + " "); } Console.WriteLine(); } binaryWriter.Close(); fs.Close(); MessageBox.Show("Conversion Complete."); }