

function img = img_gradient_d(width, height, colour1, colour2, steps, direction)                                    
a1 = colour1(1);
a2 = colour1(2);
a3 = colour1(3);

b1 = colour2(1);
b2 = colour2(2);
b3 = colour2(3);

grad = zeros(height,width,3);                                              

f1 = linspace(a1,b1,steps);                                                
round(f1);                                                                 
f2 = linspace(a2,b2,steps);                                                
round(f2);                                                                 
f3 = linspace(a3,b3,steps);                                               
round(f3);                                                                 

switch direction                                                           
    case 'vertical'
        p = ceil(height/steps);
    otherwise
        p = ceil(width/steps);
end  

s = steps*p;

switch direction                                                           
    case 'vertical'
        for n = 1:steps-2                                                   
            grad(n*p:(n+1)*p,:,1) = f1(n+1);                                
            grad(n*p:(n+1)*p,:,2) = f2(n+1);                                
            grad(n*p:(n+1)*p,:,3) = f3(n+1);                                
        end
        grad(1:p,:,1) = f1(1);
        grad(1:p,:,2) = f2(1);
        grad(1:p,:,3) = f3(1);
        for s > height% ?????
            grad((steps-1)*p:height,:,1) = f1(steps);
            grad((steps-1)*p:height,:,2) = f2(steps);
            grad((steps-1)*p:height,:,3) = f3(steps);
        end
    otherwise
        for n = 1:steps-2
            grad(:,n*p:(n+1)*p,1) = f1(n+1);
            grad(:,n*p:(n+1)*p,2) = f2(n+1);
            grad(:,n*p:(n+1)*p,3) = f3(n+1);
        end
        grad(:,1:p,1) = f1(1);
        grad(:,1:p,2) = f2(1);
        grad(:,1:p,3) = f3(1);    
        for s > width%?????
            grad(:,(steps-1)*p:width,1) = f1(steps);
            grad(:,(steps-1)*p:width,2) = f2(steps);
            grad(:,(steps-1)*p:width,3) = f3(steps);
        end
end

img = uint8(grad);
imagesc(img);
