Table of Contents

Class ColorList

Namespace
CADability.Attribute
Assembly
CADability.dll

A table of named colours (ColorDef objects). The table is serializable and is usually kept in a Project or in the global Settings. It can be edited interactively in the property grid, where it also handles the context menu commands for its entries.

public class ColorList : PropertyEntryImpl, IPropertyEntry, IShowProperty, ISerializable, INotifyModification, ICollection, IEnumerable, IAttributeList, ICommandHandler, IDeserializationCallback, IJsonSerialize, IJsonSerializeDone
Inheritance
ColorList
Implements
Inherited Members

Constructors

ColorList()

Creates an empty ColorList.

public ColorList()

ColorList(SerializationInfo, StreamingContext)

Constructor required by deserialization

protected ColorList(SerializationInfo info, StreamingContext context)

Parameters

info SerializationInfo

SerializationInfo

context StreamingContext

StreamingContext

Properties

ContextMenu

Must be overridden, when Flags contains PropertyEntryType.ContextMenu. See ContextMenu

public override MenuWithHandler[] ContextMenu { get; }

Property Value

MenuWithHandler[]

Count

The number of named colours in the list; the placeholders enabled by Usage are not counted.

public int Count { get; }

Property Value

int

Current

The current colour: the colour that newly constructed objects get (see SetDefaults(IGeoObject)), shown in bold in the control center. When no current colour has been set, the first colour of the list is used. Setting a ColorDef that is not yet in the list adds it; null is ignored.

public ColorDef Current { get; set; }

Property Value

ColorDef

CurrentIndex

The index of the current colour (see Current) in the indexing of GetName(int). Setting it makes the entry at that index current; an index outside the valid range is ignored.

public int CurrentIndex { get; set; }

Property Value

int

Flags

Implementation of Flags: the derived class states what the entry is and what the user can do with it. The flags must match the overridden members, e.g. HasSubEntries requires SubItems, ValueEditable requires Value and EndEdit(bool, bool, string), DropDown requires GetDropDownList() and ListBoxSelected(int).

public override PropertyEntryType Flags { get; }

Property Value

PropertyEntryType

IsSynchronized

Implements IsSynchronized. The list is not synchronized, the value is false.

public bool IsSynchronized { get; }

Property Value

bool

this[int]

Returns the ColorDef object at the given index. The index of a name can be found with the IndexOf(string) method.

public ColorDef this[int Index] { get; }

Parameters

Index int

Index of the requested object

Property Value

ColorDef

The ColorDef at that index

Names

The names of all entries in the order of the list, followed by the names of the placeholders CDfromParent and CDfromStyle when Usage allows them. This is the list of choices of a ColorSelectionProperty.

public string[] Names { get; }

Property Value

string[]

Owner

The Project or Settings that holds this list. It is set by the owner when the list is added to it; the list asks it before removing a colour (see RemoveAt(int)) and forwards changes of its colours to it.

public IAttributeListContainer Owner { get; set; }

Property Value

IAttributeListContainer

SubItems

Must be overridden, when Flags contains PropertyEntryType.HasSubEntries. See SubItems

public override IPropertyEntry[] SubItems { get; }

Property Value

IPropertyEntry[]

SyncRoot

An object that can be used to lock the list for synchronized access (SyncRoot).

public object SyncRoot { get; }

Property Value

object

Usage

Controls whether the placeholders CDfromParent and CDfromStyle are visible through Names, Find(string), IndexOf(string), GetName(int) and the indexer, as if they were entries at the end of the list. A ColorSelectionProperty sets this temporarily to the flags it was constructed with.

public ColorList.StaticFlags Usage { get; set; }

Property Value

ColorList.StaticFlags

Methods

Add(ColorDef)

Adds a ColorDef to the end of the list and makes this list its parent. The first colour added becomes the current colour. Raises the DidModifyEvent.

public void Add(ColorDef cd)

Parameters

cd ColorDef

the colour to add

Exceptions

NameAlreadyExistsException

when the list already contains an entry with the same name

AddColor(string, Color)

Adds a color, or overwrites an existing one if the name already exists.

public int AddColor(string Name, Color Color)

Parameters

Name string

Name of the color.

Color Color

RGB (or ARGB) value of the color.

Returns

int

Index of the new or modified entry.

CopyTo(Array, int)

Copies the named colours, in the order of the list, to the given array (CopyTo(Array, int)).

public void CopyTo(Array array, int index)

Parameters

array Array

the destination array, which must be large enough

index int

the index in array at which copying begins

CreateOrFind(string, Color)

Returns the named colour with the given name; when there is none, a new ColorDef with that name and the given colour value is added to the list and returned. The colour value of an existing entry is left as it is (see CreateOrModify(string, Color)). This is the usual way to get a colour for an object: project.ColorList.CreateOrFind("red", Color.Red).

public ColorDef CreateOrFind(string Name, Color Color)

Parameters

Name string

the name of the colour; null for a new, automatically named entry (see GetNewName())

Color Color

the colour value for a newly created entry

Returns

ColorDef

the existing or newly created ColorDef

CreateOrModify(string, Color)

Returns the named colour with the given name, setting its colour value to Color; when there is none, a new ColorDef with that name and colour is added to the list and returned.

public ColorDef CreateOrModify(string Name, Color Color)

Parameters

Name string

the name of the colour

Color Color

the colour value

Returns

ColorDef

the existing (modified) or newly created ColorDef

Find(string)

public ColorDef Find(string Name)

Parameters

Name string

Returns

ColorDef

FindColor(Color)

Returns the first named colour of the list whose Color equals the given colour. Note that the equality of Color also compares whether it is a known (named) colour, so e.g. Color.Red does not match an entry created from the values (255, 0, 0).

public ColorDef FindColor(Color rgb)

Parameters

rgb Color

the colour value to look for

Returns

ColorDef

the ColorDef, or null when no entry has this colour

FindIndex(ColorDef)

Returns the index of the given ColorDef object in the list.

public int FindIndex(ColorDef cd)

Parameters

cd ColorDef

the ColorDef to look for (compared by reference)

Returns

int

the index, or -1 when the object is not in this list

GetColor(int)

Returns the RGB colour of the entry at the given index. For an index outside the list the colour blue is returned.

public Color GetColor(int Index)

Parameters

Index int

Index of the entry

Returns

Color

The colour

GetDefault()

Creates a colour list with the colours defined by the string resource "ColorList.Default" (red, green and blue in the standard resources); the first colour is the current one. Used for the global settings when no colour list has been saved yet.

public static ColorList GetDefault()

Returns

ColorList

the default colour list

GetEnumerator()

Enumerates the named colours in the order of the list, so foreach (ColorDef cd in colorList) can be used. The placeholders enabled by Usage are not included.

public IEnumerator GetEnumerator()

Returns

IEnumerator

an enumerator over the ColorDef objects

GetName(int)

Returns the name of the given index.

public string GetName(int Index)

Parameters

Index int

The index.

Returns

string

The name.

GetNewName()

Builds a name for a new colour from the string resource "ColorList.NewColorName" ("Color" in the English resources) and a number, see GetNewName(string). Used when a colour is created without a name.

public string GetNewName()

Returns

string

the new name

GetNewName(string)

Builds a name that is not yet used in the list: startWith followed by a number that is one greater than the highest number any existing name with this prefix ends with (e.g. "Color3" when "Color1" and "Color2" exist).

public string GetNewName(string startWith)

Parameters

startWith string

the prefix of the name

Returns

string

the new name

GetNextColorDef(string)

Returns the entry following the entry with the given name, to cycle through the colours of the list: after the last named colour comes CDfromStyle when Usage allows it, then the first entry again. Not used by CADability itself.

public ColorDef GetNextColorDef(string Name)

Parameters

Name string

the name of the current entry

Returns

ColorDef

the next ColorDef; the first entry when the name is not found

GetObjectData(IJsonWriteData)

Writes the colours and the current colour for the JSON serialization (IJsonSerialize).

public void GetObjectData(IJsonWriteData data)

Parameters

data IJsonWriteData

receives the data

GetObjectData(SerializationInfo, StreamingContext)

Writes the data of this object for the legacy binary serialization (ISerializable). Files are written in the JSON format (IJsonSerialize) now; the binary format is still read.

public void GetObjectData(SerializationInfo info, StreamingContext context)

Parameters

info SerializationInfo

receives the data

context StreamingContext

the destination of the serialization

IndexOf(string)

Returns the index of the given name.

public int IndexOf(string Name)

Parameters

Name string

The name of the color to look for.

Returns

int

Index of the color or -1, if there is no color with this name in the table.

IsStatic(int)

Tells whether the given index addresses one of the static placeholder entries (CDfromParent, CDfromStyle), which follow the named colours in the indexing of GetName(int) and the indexer when enabled by Usage. Those entries cannot be renamed or recoloured.

public bool IsStatic(int idx)

Parameters

idx int

the index to test

Returns

bool

true for an index beyond the named colours

RemoveAt(int)

Removes the colour at the given index. If the list belongs to a project or to the settings, the owner is asked first and may refuse the removal (e.g. when the colour is still in use). Raises the DidModifyEvent.

public void RemoveAt(int Index)

Parameters

Index int

Index of the colour to remove

SerializationDone(JsonSerialize)

Callback of the JSON serialization (IJsonSerializeDone): after all objects have been read, the back references of the colours to this list, which are not stored in the file, are restored.

public void SerializationDone(JsonSerialize jsonSerialize)

Parameters

jsonSerialize JsonSerialize

the serializer

SetColor(int, Color)

Changes the RGB colour of the entry at the given index. Raises the DidModifyEvent.

public void SetColor(int Index, Color Color)

Parameters

Index int

Index of the entry to change

Color Color

The new colour value

SetName(int, string)

Changes the name of the entry at the given index. The change is rejected when the new name is already used by another entry of the list. Raises the DidModifyEvent.

public void SetName(int Index, string Name)

Parameters

Index int

Index of the entry to change

Name string

The new name

SetObjectData(IJsonReadData)

Restores the colours and the current colour from the JSON data written by GetObjectData(IJsonWriteData) (IJsonSerialize).

public void SetObjectData(IJsonReadData data)

Parameters

data IJsonReadData

the data to read from

Events

DidModifyEvent

Raised when a colour is added, removed, renamed or recoloured through the methods of this list (INotifyModification). Changes made directly on a ColorDef are forwarded to the Owner instead.

public event DidModifyDelegate DidModifyEvent

Event Type

DidModifyDelegate