Archive Browser
Download CorelDrawPalPaletteLoader.zip, last updated 21/07/2018 (17.87 KB)
Download- md5: d087b521549c15bead0178e98453caf8
- sha1: 1d576767f47ca15976ccba8a56922da4793e7e5a
- sha256: 24bdc574d1725993cae5543da51745037853dbf72c7e60e5ecf4f635d9374858
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
// Reading CorelDRAW Palettes Part 1, .pal files
// https://www.cyotek.com/blog/reading-coreldraw-palettes-part-1-pal-files
// Copyright © 2018 Cyotek Ltd. All Rights Reserved.
// This work is licensed under the Creative Commons Attribution 4.0 International License.
// To view a copy of this license, visit http://creativecommons.org/licenses/by/4.0/.
// Found this example useful?
// https://www.paypal.me/cyotek
namespace Cyotek.Demonstrations.CorelDrawPaletteLoader
{
internal sealed class CorelDrawPalette : IEnumerable<Color>
{
#region Constants
private readonly List<Color> _palette;
private readonly char[] _whitespace =
{
' '
};
#endregion
#region Constructors
public CorelDrawPalette()
{
_palette = new List<Color>();
}
public CorelDrawPalette(IEnumerable<Color> colors)
{
if (colors == null)
{
throw new ArgumentNullException(nameof(colors));
}
_palette = new List<Color>(colors);
}
#endregion
#region Static Methods
public static CorelDrawPalette LoadFrom(string fileName)
{
CorelDrawPalette palette;
palette = new CorelDrawPalette();
palette.Load(fileName);
return palette;
}
public static CorelDrawPalette LoadFrom(Stream stream)
{
CorelDrawPalette palette;
palette = new CorelDrawPalette();
palette.Load(stream);
return palette;
}
#endregion
#region Properties
public int Count
{
get { return _palette.Count; }
}
public Color this[int index]
{
get { return _palette[index]; }
}
#endregion
#region Methods
public void Load(string fileName)
{
using (Stream stream = File.OpenRead(fileName))
{
this.Load(stream);
}
}
public void Load(Stream stream)
{
_palette.Clear();
using (TextReader reader = new StreamReader(stream, Encoding.ASCII))
{
string line;
while ((line = reader.ReadLine()) != null && (line.Length == 0 || line[0] != (char)26))
{
if (!string.IsNullOrEmpty(line))
{
int lastQuote;
int numberPosition;
byte cyan;
byte magenta;
byte yellow;
byte black;
//string name;
lastQuote = line.LastIndexOf('"');
if (lastQuote == -1)
{
throw new InvalidDataException("Unable to parse line.");
}
//name = line.Substring(1, lastQuote - 1);
numberPosition = lastQuote + 1;
cyan = this.NextNumber(line, ref numberPosition);
magenta = this.NextNumber(line, ref numberPosition);
yellow = this.NextNumber(line, ref numberPosition);
black = this.NextNumber(line, ref numberPosition);
//string[] parts;
//parts = line.Substring(lastQuote + 1).Split(_whitespace, StringSplitOptions.RemoveEmptyEntries);
//cyan = byte.Parse(parts[0]);
//magenta = byte.Parse(parts[1]);
//yellow = byte.Parse(parts[2]);
//black = byte.Parse(parts[3]);
_palette.Add(this.ConvertCmykToRgb(cyan, magenta, yellow, black));
}
}
}
}
public Color[] ToArray()
{
return _palette.ToArray();
}
private Color ConvertCmykToRgb(int c, int m, int y, int k)
{
int r;
int g;
int b;
float multiplier;
multiplier = 1 - k / 100F;
r = Convert.ToInt32(255 * (1 - c / 100F) * multiplier);
g = Convert.ToInt32(255 * (1 - m / 100F) * multiplier);
b = Convert.ToInt32(255 * (1 - y / 100F) * multiplier);
return Color.FromArgb(r, g, b);
}
private byte NextNumber(string line, ref int start)
{
int length;
int valueLength;
int maxLength;
byte result;
// skip any leading spaces
while (char.IsWhiteSpace(line[start]))
{
start++;
}
length = line.Length;
maxLength = Math.Min(3, length - start);
valueLength = 0;
for (int i = 0; i < maxLength; i++)
{
if (char.IsDigit(line[start + i]))
{
valueLength++;
}
else
{
break;
}
}
result = byte.Parse(line.Substring(start, valueLength));
start += valueLength;
return result;
}
#endregion
#region IEnumerable<Color> Interface
public IEnumerator<Color> GetEnumerator()
{
return ((IEnumerable<Color>)_palette).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
#endregion
}
}
Donate
This software may be used free of charge, but as with all free software there are costs involved to develop and maintain.
If this site or its services have saved you time, please consider a donation to help with running costs and timely updates.
Donate