/// Gets or sets the number of color levels within each color group. The Minecraft default is 4.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the property is assigned a non-positive value.</exception>
publicintColorGroupSize
{
get{return_groupSize;}
set
{
if(value<=0){
thrownewArgumentOutOfRangeException("The ColorGroupSize property must be a positive number.");
}
_groupSize=value;
}
}
/// <summary>
/// Gets the color index table, used to translate map color index values to RGB color values.
/// </summary>
publicColor[]ColorIndex
{
get{return_colorIndex;}
}
/// <summary>
/// Gets the block index table, used to translate block IDs to <see cref="ColorGroup"/>s that represent them.
/// </summary>
publicColorGroup[]BlockIndex
{
get{return_blockIndex;}
}
/// <summary>
/// Returns the baseline color index associated with the <see cref="ColorGroup"/> currently representing the given block ID.
/// </summary>
/// <param name="blockId">The ID of a block.</param>
/// <returns>A color index value.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="blockId"/> is out of its normal range.</exception>
publicintBlockToColorIndex(intblockId)
{
returnBlockToColorIndex(blockId,0);
}
/// <summary>
/// Returns a color index associated with the <see cref="ColorGroup"/> currently representing the given block ID and based on the given level.
/// </summary>
/// <param name="blockId">The ID of a block.</param>
/// <param name="level">The color level to select from within the derived <see cref="ColorGroup"/>.</param>
/// <returns>A color index value.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when either <paramref name="blockId"/> or <paramref name="level"/> are out of their normal ranges.</exception>
publicintBlockToColorIndex(intblockId,intlevel){
if(level<0||level>=_groupSize){
thrownewArgumentOutOfRangeException("level",level,"Argument 'level' must be in range [0, "+(_groupSize-1)+"]");
}
if(blockId<0||blockId>=256){
thrownewArgumentOutOfRangeException("blockId");
}
return(int)_blockIndex[blockId]*_groupSize+level;
}
/// <summary>
/// Returns a <see cref="Color"/> value assocated with the <see cref="ColorGroup"/> currently representing the given block ID.
/// </summary>
/// <param name="blockId">The ID of a block.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="blockId"/> is out of its normal range.</exception>
/// <exception cref="InvalidOperationException">Thrown when <paramref name="blockId"/> maps to an invalid color index.</exception>
publicColorBlockToColor(intblockId)
{
returnBlockToColor(blockId,0);
}
/// <summary>
/// Returns a <see cref="Color"/> value associated with the <see cref="ColorGroup"/> currently representing the given block ID and based on the given level.
/// </summary>
/// <param name="blockId">The ID of a block.</param>
/// <param name="level">The color level to select from within the derived <see cref="ColorGroup"/>.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when either <paramref name="blockId"/> or <paramref name="level"/> are out of their normal ranges.</exception>
/// <exception cref="InvalidOperationException">Thrown when <paramref name="blockId"/> maps to an invalid color index.</exception>
publicColorBlockToColor(intblockId,intlevel)
{
intci=BlockToColorIndex(blockId,level);
if(ci<0||ci>=256){
thrownewInvalidOperationException("The specified Block ID mapped to an invalid color index.");
}
return_colorIndex[ci];
}
/// <summary>
/// Returns the <see cref="ColorGroup"/> that a particular color index is part of.
/// </summary>
/// <param name="colorIndex">A color index value.</param>
/// <param name="level">A level value within the <see cref="ColorGroup"/>.</param>
/// <returns>The color index value for the given <see cref="ColorGroup"/> and group level.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the <paramref name="level"/> is out of range with respect to the current <see cref="ColorGroupSize"/> parameter.</exception>
/// <param name="level">A level value within the <see cref="ColorGroup"/> and group level.</param>
/// <returns>The <see cref="Color"/> for the given <see cref="ColorGroup"/> and group level.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the <paramref name="level"/> is out of range with respect to the current <see cref="ColorGroupSize"/> parameter.</exception>
/// <exception cref="InvalidOperationException">Thrown when the <paramref name="group"/> and <paramref name="level"/> map to an invalid color index.</exception>
publicColorGroupToColor(ColorGroupgroup,intlevel)
{
intci=GroupToColorIndex(group,level);
if(ci<0||ci>=256){
thrownewInvalidOperationException("The specified group mapped to an invalid color index.");
}
return_colorIndex[ci];
}
/// <summary>
/// Rebuilds the internal color conversion tables. Should be called after modifying the <see cref="ColorIndex"/> table.
/// </summary>
publicvoidRefreshColorCache()
{
for(inti=0;i<_colorIndex.Length;i++){
_labIndex[i]=RgbToLab(_colorIndex[i]);
}
}
/// <summary>
/// Given a <see cref="Color"/>, returns the index of the closest matching color from the color index table.
/// <exception cref="InvalidOperationException">Thrown when the <paramref name="map"/> and <paramref name="bmp"/> objects have different dimensions.</exception>
publicvoidBitmapToMap(Mapmap,Bitmapbmp)
{
if(map.Width!=bmp.Width||map.Height!=bmp.Height){
thrownewInvalidOperationException("The source map and bitmap must have the same dimensions.");
}
for(intx=0;x<map.Width;x++){
for(intz=0;z<map.Height;z++){
Colorc=bmp.GetPixel(x,z);
map[x,z]=(byte)NearestColorIndex(c);
}
}
}
/// <summary>
/// Creates a 32bpp <see cref="Bitmap"/> from a <see cref="Map"/>.