public override ImageDatabase.Element GetNearest(Color color) { KDBox box = new KDBox(0, 0, 0, 255, 255, 255); return root.NN(box, color, null).data; }
public Node NN(KDBox box, Color color, Node best) { double bestDiff = 0; double boxDiff = 0; if (best != null) { bestDiff = ImageDatabase.ColorDiff(color, best.data.color); boxDiff = box.Distance(color); } if (best == null || bestDiff > boxDiff) { if (best == null || bestDiff > ImageDatabase.ColorDiff(color, data.color)) best = this; KDBox leftBox = new KDBox( GetColorDimension(0, box.lowest), GetColorDimension(1, box.lowest), GetColorDimension(2, box.lowest), GetColorDimension(0, dimension == 0 ? data.color : box.highest), GetColorDimension(1, dimension == 1 ? data.color : box.highest), GetColorDimension(2, dimension == 2 ? data.color : box.highest) ); KDBox rightBox = new KDBox( GetColorDimension(0, dimension == 0 ? data.color : box.lowest), GetColorDimension(1, dimension == 1 ? data.color : box.lowest), GetColorDimension(2, dimension == 2 ? data.color : box.lowest), GetColorDimension(0, box.highest), GetColorDimension(1, box.highest), GetColorDimension(2, box.highest) ); if (GetColorDimension(dimension, color) < GetColorDimension(dimension, data.color)) { if (children[0] != null) { best = children[0].NN(leftBox, color, best); } if (children[1] != null) { best = children[1].NN(rightBox, color, best); } } else { if (children[1] != null) { best = children[1].NN(rightBox, color, best); } if (children[0] != null) { best = children[0].NN(leftBox, color, best); } } } return best; }