function Lab10()
close all
A = imread('Cornell_Clock.jpg');
[m,n,r] = size(A);
imshow(A)
for i=1:m
for j = 1:n
% Replace the value of pixel (i,j) with its nearest neighbor
% in the 8-color library...
q = Closest([A(i,j,1) A(i,j,2) A(i,j,3)]);
A(i,j,1) = q(1); A(i,j,2) = q(2); A(i,j,3) = q(3);
end
end
figure
imshow(A)
shg

function d = pixelDist(p,q)
% p and q are uint8 3-vectors (row orientation)
% d = |p(1)-q(1)|+|p(2)-q(2)|+|p(3)-q(3)|
d = sum(abs(double(p)-double(q)));

function q = Closest(p)
% p is a uint8 3-vector
% q is the closest row of
colors = uint8(255*[1 0 0; 0 1 0; 0 0 1; 1 0 1; 0 1 1; 1 1 0; 1 1 1; 0 0 0]);
d = zeros(8,1);
for k=1:8
d(k) = pixelDist(p,colors(k,:));
end
[ds,idx] = sort(d);
% the index of the closest color...
i = idx(1);
q = colors(i,:);