1 // 2 // Palette.cs 3 // 4 // Author: 5 // Maia Kozheva <sikon@ubuntu.com> 6 // 7 // Copyright (c) 2010 Maia Kozheva <sikon@ubuntu.com> 8 // 9 // Permission is hereby granted, free of charge, to any person obtaining a copy 10 // of this software and associated documentation files (the "Software"), to deal 11 // in the Software without restriction, including without limitation the rights 12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 // copies of the Software, and to permit persons to whom the Software is 14 // furnished to do so, subject to the following conditions: 15 // 16 // The above copyright notice and this permission notice shall be included in 17 // all copies or substantial portions of the Software. 18 // 19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 // THE SOFTWARE. 26 27 using System; 28 using System.Collections.Generic; 29 using System.Globalization; 30 using System.IO; 31 using System.Linq; 32 using Cairo; 33 using Gtk; 34 using Mono.Unix; 35 36 namespace Pinta.Core 37 { 38 public sealed class Palette 39 { 40 public event EventHandler PaletteChanged; 41 42 private List<Color> colors; 43 Palette()44 private Palette () 45 { 46 colors = new List<Color> (); 47 } 48 OnPaletteChanged()49 private void OnPaletteChanged () 50 { 51 if (PaletteChanged != null) 52 PaletteChanged (this, EventArgs.Empty); 53 } 54 55 public int Count 56 { 57 get { 58 return colors.Count; 59 } 60 } 61 62 public Color this[int index] 63 { 64 get { 65 return colors[index]; 66 } 67 68 set { 69 colors[index] = value; 70 OnPaletteChanged (); 71 } 72 } 73 Resize(int newSize)74 public void Resize (int newSize) 75 { 76 int difference = newSize - Count; 77 78 if (difference > 0) { 79 for (int i = 0; i < difference; i++) 80 colors.Add (new Color (1, 1, 1)); 81 } else { 82 colors.RemoveRange (newSize, -difference); 83 } 84 85 colors.TrimExcess (); 86 OnPaletteChanged (); 87 } 88 GetDefault()89 public static Palette GetDefault () 90 { 91 Palette p = new Palette (); 92 p.LoadDefault (); 93 return p; 94 } 95 FromFile(string fileName)96 public static Palette FromFile (string fileName) 97 { 98 Palette p = new Palette (); 99 p.Load (fileName); 100 return p; 101 } 102 LoadDefault()103 public void LoadDefault () 104 { 105 colors.Clear (); 106 107 colors.Add (new Color (255 / 255f, 255 / 255f, 255 / 255f)); 108 colors.Add (new Color (128 / 255f, 128 / 255f, 128 / 255f)); 109 colors.Add (new Color (127 / 255f, 0 / 255f, 0 / 255f)); 110 colors.Add (new Color (127 / 255f, 51 / 255f, 0 / 255f)); 111 colors.Add (new Color (127 / 255f, 106 / 255f, 0 / 255f)); 112 colors.Add (new Color (91 / 255f, 127 / 255f, 0 / 255f)); 113 colors.Add (new Color (38 / 255f, 127 / 255f, 0 / 255f)); 114 colors.Add (new Color (0 / 255f, 127 / 255f, 14 / 255f)); 115 colors.Add (new Color (0 / 255f, 127 / 255f, 70 / 255f)); 116 colors.Add (new Color (0 / 255f, 127 / 255f, 127 / 255f)); 117 colors.Add (new Color (0 / 255f, 74 / 255f, 127 / 255f)); 118 colors.Add (new Color (0 / 255f, 19 / 255f, 127 / 255f)); 119 colors.Add (new Color (33 / 255f, 0 / 255f, 127 / 255f)); 120 colors.Add (new Color (87 / 255f, 0 / 255f, 127 / 255f)); 121 colors.Add (new Color (127 / 255f, 0 / 255f, 110 / 255f)); 122 colors.Add (new Color (127 / 255f, 0 / 255f, 55 / 255f)); 123 124 colors.Add (new Color (0 / 255f, 0 / 255f, 0 / 255f)); 125 colors.Add (new Color (64 / 255f, 64 / 255f, 64 / 255f)); 126 colors.Add (new Color (255 / 255f, 0 / 255f, 0 / 255f)); 127 colors.Add (new Color (255 / 255f, 106 / 255f, 0 / 255f)); 128 colors.Add (new Color (255 / 255f, 216 / 255f, 0 / 255f)); 129 colors.Add (new Color (182 / 255f, 255 / 255f, 0 / 255f)); 130 colors.Add (new Color (76 / 255f, 255 / 255f, 0 / 255f)); 131 colors.Add (new Color (0 / 255f, 255 / 255f, 33 / 255f)); 132 colors.Add (new Color (0 / 255f, 255 / 255f, 144 / 255f)); 133 colors.Add (new Color (0 / 255f, 255 / 255f, 255 / 255f)); 134 colors.Add (new Color (0 / 255f, 148 / 255f, 255 / 255f)); 135 colors.Add (new Color (0 / 255f, 38 / 255f, 255 / 255f)); 136 colors.Add (new Color (72 / 255f, 0 / 255f, 255 / 255f)); 137 colors.Add (new Color (178 / 255f, 0 / 255f, 255 / 255f)); 138 colors.Add (new Color (255 / 255f, 0 / 255f, 220 / 255f)); 139 colors.Add (new Color (255 / 255f, 0 / 255f, 110 / 255f)); 140 141 colors.Add (new Color (160 / 255f, 160 / 255f, 160 / 255f)); 142 colors.Add (new Color (48 / 255f, 48 / 255f, 48 / 255f)); 143 colors.Add (new Color (255 / 255f, 127 / 255f, 127 / 255f)); 144 colors.Add (new Color (255 / 255f, 178 / 255f, 127 / 255f)); 145 colors.Add (new Color (255 / 255f, 233 / 255f, 127 / 255f)); 146 colors.Add (new Color (218 / 255f, 255 / 255f, 127 / 255f)); 147 colors.Add (new Color (165 / 255f, 255 / 255f, 127 / 255f)); 148 colors.Add (new Color (127 / 255f, 255 / 255f, 142 / 255f)); 149 colors.Add (new Color (127 / 255f, 255 / 255f, 197 / 255f)); 150 colors.Add (new Color (127 / 255f, 255 / 255f, 255 / 255f)); 151 colors.Add (new Color (127 / 255f, 201 / 255f, 255 / 255f)); 152 colors.Add (new Color (127 / 255f, 146 / 255f, 255 / 255f)); 153 colors.Add (new Color (161 / 255f, 127 / 255f, 255 / 255f)); 154 colors.Add (new Color (214 / 255f, 127 / 255f, 255 / 255f)); 155 colors.Add (new Color (255 / 255f, 127 / 255f, 237 / 255f)); 156 colors.Add (new Color (255 / 255f, 127 / 255f, 182 / 255f)); 157 158 colors.TrimExcess (); 159 OnPaletteChanged (); 160 } 161 Load(string fileName)162 public void Load (string fileName) 163 { 164 try 165 { 166 var loader = PintaCore.System.PaletteFormats.GetLoaderByFilename (fileName); 167 168 if (loader == null) 169 throw new FormatException(); 170 171 colors = loader.Load (fileName); 172 colors.TrimExcess (); 173 OnPaletteChanged (); 174 } 175 catch (FormatException e) 176 { 177 var parent = PintaCore.Chrome.MainWindow; 178 ShowUnsupportedFormatDialog(parent, fileName, "Unsupported palette format", e.ToString()); 179 } 180 } 181 Save(string fileName, IPaletteSaver saver)182 public void Save (string fileName, IPaletteSaver saver) 183 { 184 saver.Save (colors, fileName); 185 } 186 ShowUnsupportedFormatDialog(Window parent, string filename, string primaryText, string details)187 private void ShowUnsupportedFormatDialog(Window parent, string filename, string primaryText, string details) 188 { 189 string markup = "<span weight=\"bold\" size=\"larger\">{0}</span>\n\n{1}"; 190 191 string secondaryText = string.Format(Catalog.GetString("Could not open file: {0}"), filename); 192 secondaryText += string.Format("\n\n{0}\n", Catalog.GetString("Pinta supports the following palette formats:")); 193 var extensions = from format in PintaCore.System.PaletteFormats.Formats 194 where format.Loader != null 195 from extension in format.Extensions 196 where char.IsLower(extension.FirstOrDefault()) 197 orderby extension 198 select extension; 199 200 secondaryText += String.Join(", ", extensions); 201 202 string message = string.Format(markup, primaryText, secondaryText); 203 PintaCore.Chrome.ShowUnsupportedFormatDialog(parent, message, details); 204 } 205 } 206 } 207