public static async Task ReplaceColorAsync(this IColorMatrix sourceColorMatrix, ColorItem oldColorItem, Color newColor, bool convertToBackground = false) { Color oldColor = oldColorItem; for (uint row = 0; row < sourceColorMatrix.Height; row++) { for (uint column = 0; column < sourceColorMatrix.Width; column++) { if (sourceColorMatrix.ColorItems[row, column] == oldColor && sourceColorMatrix.ColorItems[row, column].ItemType == oldColorItem.ItemType) { ColorItem newItem = new ColorItem() { A = newColor.A, R = newColor.R, G = newColor.G, B = newColor.B, ItemType = convertToBackground ? ColorItem.ColorItemType.Background : sourceColorMatrix.ColorItems[row, column].ItemType }; await sourceColorMatrix.SetItem(row, column, newItem); } } } }
public static async Task Clear(this IColorMatrix sourceColorMatrix, Color color) { for (uint row = 0; row < sourceColorMatrix.Height; row++) { for (uint column = 0; column < sourceColorMatrix.Width; column++) { await sourceColorMatrix.SetItem(row, column, ColorItem.FromColor(color, ColorItem.ColorItemType.Background)); } } }
public static async Task CopyTo(this IColorMatrix sourceColorMatrix, IColorMatrix targetColorMatrix) { for (uint row = 0; row < sourceColorMatrix.Height; row++) { for (uint column = 0; column < sourceColorMatrix.Width; column++) { await targetColorMatrix.SetItem(row, column, sourceColorMatrix.ColorItems[row, column]); } } }
public static async Task FlipHorizontalAsync(this IColorMatrix sourceColorMatrix) { IColorMatrix me = await sourceColorMatrix.CloneAsync(); for (uint row = 0; row < sourceColorMatrix.Height; row++) { for (uint column = 0; column < sourceColorMatrix.Width; column++) { await sourceColorMatrix.SetItem(row, column, me.ColorItems[row, (sourceColorMatrix.Width - 1) - column]); } } }
public static async Task RotateClockwiseAsync(this IColorMatrix sourceColorMatrix) { IColorMatrix me = await sourceColorMatrix.CloneAsync(); for (uint row = 0; row < sourceColorMatrix.Height; row++) { for (uint column = 0; column < sourceColorMatrix.Width; column++) { await sourceColorMatrix.SetItem(row, column, me.ColorItems[(sourceColorMatrix.Height - 1) - column, row]); } } }
public static Task <IColorMatrix> CloneAsync(this IColorMatrix sourceColorMatrix) { IColorMatrix returnValue = new ColorMatrix(sourceColorMatrix.Width, sourceColorMatrix.Height); for (uint row = 0; row < sourceColorMatrix.Height; row++) { for (uint column = 0; column < sourceColorMatrix.Width; column++) { returnValue.ColorItems[column, row] = sourceColorMatrix.ColorItems[column, row]; } } return(Task.FromResult(returnValue)); }
public static uint GetPixelCount(this IColorMatrix colorMatrix, ColorItem.ColorItemType itemType) { uint returnValue = 0; for (uint row = 0; row < colorMatrix.Height; row++) { for (uint column = 0; column < colorMatrix.Width; column++) { if (colorMatrix.ColorItems[row, column].ItemType == itemType) { returnValue++; } } } return(returnValue); }
public static Task <IList <ColorItem> > GetPaletteAsync(this IColorMatrix sourceColorMatrix) { IList <ColorItem> colors = new List <ColorItem>(); for (int row = 0; row < sourceColorMatrix.Height; row++) { for (int column = 0; column < sourceColorMatrix.Width; column++) { ColorItem color = sourceColorMatrix.ColorItems[row, column]; if (!colors.Contains(color)) { colors.Add(color); } } } return(Task.FromResult(colors)); }
/// <summary> /// Convert the ColorMatrix to a BGRA array. /// </summary> /// <returns>Image data in BGRA pixel format.</returns> public static Task <byte[]> CreateImageDataAsync(this IColorMatrix sourceColorMatrix) { byte[] returnValue = new byte[sourceColorMatrix.Width * sourceColorMatrix.Height * 4]; int index = 0; for (int row = 0; row < sourceColorMatrix.Height; row++) { for (int column = 0; column < sourceColorMatrix.Width; column++) { Color color = sourceColorMatrix.ColorItems[row, column]; // *** // *** Remove background and sand pixels. // *** if (sourceColorMatrix.ColorItems[row, column].ItemType == ColorItem.ColorItemType.Background) { // *** // *** Clear the color for transparent pixels. // *** color.A = 0; color.R = 0; color.G = 0; color.B = 0; } else if (sourceColorMatrix.ColorItems[row, column].ItemType == ColorItem.ColorItemType.Sand) { // *** // *** Leave the and color in place // *** color.A = 0; } returnValue[index + 0] = color.B; returnValue[index + 1] = color.G; returnValue[index + 2] = color.R; returnValue[index + 3] = color.A; index += 4; } } return(Task.FromResult(returnValue)); }
public static async Task ReplacePixelTypeColorAsync(this IColorMatrix sourceColorMatrix, ColorItem.ColorItemType itemType, Color newColor) { for (uint row = 0; row < sourceColorMatrix.Height; row++) { for (uint column = 0; column < sourceColorMatrix.Width; column++) { if (sourceColorMatrix.ColorItems[row, column].ItemType == itemType) { ColorItem newItem = new ColorItem() { A = newColor.A, R = newColor.R, G = newColor.G, B = newColor.B }; await sourceColorMatrix.SetItem(row, column, newItem); } } } }
public async void OnClearCommand() { try { // *** // *** Get a copy of the current color matrix. // *** IColorMatrix oldColorMatrix = await this.ColorMatrix.CloneAsync(); // *** // *** Clear the matrix. // *** await this.ColorMatrix.Clear(this.BackgroundColor); // *** // *** Clear the project name. // *** string projectName = this.ProjectName; this.ProjectName = String.Empty; // *** // *** Set up the undo task. // *** async Task undoAction() { await this.ColorMatrix.CopyFrom(oldColorMatrix); this.ProjectName = projectName; } async Task redoAction() { await this.ColorMatrix.Clear(this.BackgroundColor); this.ProjectName = String.Empty; } await this.UndoService.AddUndoTask(undoAction, redoAction, "Clear"); } catch (Exception ex) { MessageDialog dialog = new MessageDialog(ex.Message, "Exception".GetLocalized()); await dialog.ShowAsync(); } }
public async void OnLoadCommand() { try { FileOpenPicker openPicker = new FileOpenPicker { ViewMode = PickerViewMode.Thumbnail, SuggestedStartLocation = PickerLocationId.PicturesLibrary }; openPicker.FileTypeFilter.Add(".jpg"); openPicker.FileTypeFilter.Add(".jpeg"); openPicker.FileTypeFilter.Add(".png"); openPicker.FileTypeFilter.Add(".bmp"); openPicker.FileTypeFilter.Add(".tif"); openPicker.FileTypeFilter.Add(".tiff"); StorageFile file = await openPicker.PickSingleFileAsync(); if (file != null) { // *** // *** Get a copy of the current color matrix. // *** IColorMatrix oldColorMatrix = await this.ColorMatrix.CloneAsync(); IMatrixProject oldProject = this.CurrentProject; // *** // *** Read the image/project from an image file. // *** IMatrixProject project = await file.LoadFromImageAsync(ImageEditorViewModel.DefaultColumnCount, ImageEditorViewModel.DefaultColumnCount); await this.ColorMatrix.CopyFrom(project.ColorMatrix); await this.ApplyProjectSettings(project); // *** // *** Set up the undo task. // *** async Task undoAction() { await this.ColorMatrix.CopyFrom(oldColorMatrix); await this.ApplyProjectSettings(oldProject); } async Task redoAction() { await this.ColorMatrix.CopyFrom(project.ColorMatrix); await this.ApplyProjectSettings(project); } await this.UndoService.AddUndoTask(undoAction, redoAction, $"Load File [{file.Name}]"); } } catch (BadImageFormatException) { MessageDialog dialog = new MessageDialog("ExceptionImageTooLarge".GetLocalized(), "Exception".GetLocalized()); await dialog.ShowAsync(); } catch (Exception ex) { MessageDialog dialog = new MessageDialog(ex.Message, "Exception".GetLocalized()); await dialog.ShowAsync(); } }
private async void PixelMatrix_PixelSelected(object sender, PixelSelectedEventArgs e) { // *** // *** Get the current mode. // *** DrawMode mode = await this.GetDrawMode(e.KeyModifiers); // *** // *** Cache the old color of this pixel. // *** ColorItem selectedItem = await this.ColorMatrix.GetItem(e.Row, e.Column); switch (mode) { case DrawMode.PickColor: { ColorItem colorItem = await this.ColorMatrix.GetItem(e.Row, e.Column); this.PixelColor = colorItem; this.PickColorIsChecked = false; if (_sandWasChecked) { this.SandIsChecked = true; } else { this.DrawIsChecked = true; } } break; case DrawMode.Draw: { if (selectedItem != this.PixelColor || selectedItem.ItemType != ColorItem.ColorItemType.Pixel) { // *** // *** Update the pixel. // *** Color color = this.PixelColor; await this.ColorMatrix.SetItem(e.Row, e.Column, color, ColorItem.ColorItemType.Pixel); // *** // *** Set up the undo task. // *** async Task undoAction() { await this.ColorMatrix.SetItem(e.Row, e.Column, selectedItem); } async Task redoAction() { await this.ColorMatrix.SetItem(e.Row, e.Column, color, ColorItem.ColorItemType.Pixel); } await this.UndoService.AddUndoTask(undoAction, redoAction, $"Set Pixel [{e.Column}, {e.Row}, {this.PixelColor.ToString()}]"); } } break; case DrawMode.Erase: { if (selectedItem != this.BackgroundColor || selectedItem.ItemType != ColorItem.ColorItemType.Background) { // *** // *** Update the pixel. // *** await this.ColorMatrix.SetItem(e.Row, e.Column, this.BackgroundColor, ColorItem.ColorItemType.Background); // *** // *** Set up the undo task. // *** async Task undoAction() { await this.ColorMatrix.SetItem(e.Row, e.Column, selectedItem); } async Task redoAction() { await this.ColorMatrix.SetItem(e.Row, e.Column, this.BackgroundColor, ColorItem.ColorItemType.Background); } await this.UndoService.AddUndoTask(undoAction, redoAction, $"Clear Pixel [{e.Column}, {e.Row}]"); } } break; case DrawMode.EraseColor: { if (selectedItem.ItemType != ColorItem.ColorItemType.Background) { // *** // *** Clone the current matrix for the undo. // *** IColorMatrix oldColorMatrix = await this.ColorMatrix.CloneAsync(); await this.ColorMatrix.ReplaceColorAsync(selectedItem, this.BackgroundColor, true); // *** // *** Set up the undo task. // *** async Task undoAction() { await this.ColorMatrix.CopyFrom(oldColorMatrix); } async Task redoAction() { await this.ColorMatrix.ReplaceColorAsync(selectedItem, this.BackgroundColor, true); } await this.UndoService.AddUndoTask(undoAction, redoAction, $"Clear Pixels [{e.Column}, {e.Row}]"); } } break; case DrawMode.Sand: { if (selectedItem != this.PixelColor || selectedItem.ItemType != ColorItem.ColorItemType.Sand) { // *** // *** Update the pixel. // *** Color color = this.PixelColor; color.A = 255; await this.ColorMatrix.SetItem(e.Row, e.Column, color, ColorItem.ColorItemType.Sand); // *** // *** Set up the undo task. // *** async Task undoAction() { await this.ColorMatrix.SetItem(e.Row, e.Column, selectedItem); } async Task redoAction() { await this.ColorMatrix.SetItem(e.Row, e.Column, color, ColorItem.ColorItemType.Sand); } await this.UndoService.AddUndoTask(undoAction, redoAction, $"Set Sand Pixel [{e.Column}, {e.Row}, {this.PixelColor.ToString()}]"); } } break; } }