Showing posts with label Shanker Dev Campus. Show all posts
Showing posts with label Shanker Dev Campus. Show all posts

RGB to HSV Conversion

Input: RGB
Output: HSV


Steps:
1. Input RGB.
2. Normalize the RGB values to be in the range [0, 1].
    r = R/255, g = G/255, b = B/255
3. Find the difference between max and min values.
    M = max(r,g,b), m = min(r,g,b). And, the difference Δ = M - m
4. Calculate hue H.
    If M = m = 0, then H = 0
    If M = r, then compute H = (60 * ((g – b) / Δ) + 360) % 360
    If M = g, then compute H = (60 * ((b – r) / Δ) + 120) % 360
    If M = b, then compute H = (60 * ((r – g) / Δ) + 240) % 360
5. Calculate saturation S.
    If Δ = 0, S = 0; Else, S = Δ/M
6. Calculate value V.
    V = max(r,g,b)
7. Output HSV.

Software Engineering (SE) Notes


Excerpt from "Software Engineering" 10th GLOBAL Edition by Ian Sommerville

Java Program - Mid-Point Circle Drawing Algorithm

import java.awt.*;
import javax.swing.JFrame;
import java.util.Scanner;
import java.lang.*;

public class MP_CIRCLE extends Canvas
{
  static int xc,yc,r;
  
  MP_CIRCLE(int xc,int yc,int r)
  {
   this.xc=xc;
   this.yc=yc;
   this.r=r;
  }
  
  public void paint(Graphics g)
  {
   int x,y,p;
   x=0;
   y=r;
   fill(g,x,y,xc,yc);
   p=1-r;
   while(x<y)
   {
    x=x+1;
    if(p<0)
    {
     p=p+2*x+1;
    }
    else
    {
     y=y-1;
     p=p+2*x+1-2*y;
    }
    fill(g,x,y,xc,yc);
   }
  }
  
  public void fill(Graphics g,int x,int y,int xc,int yc)
  {
   g.fillOval(xc+x,yc+y,5,5);
   g.fillOval(xc+x,yc-y,5,5);
   g.fillOval(xc-x,yc+y,5,5);
   g.fillOval(xc-x,yc-y,5,5);
   g.fillOval(xc+y,yc+x,5,5);
   g.fillOval(xc+y,yc-x,5,5);
   g.fillOval(xc-y,yc+x,5,5);
   g.fillOval(xc-y,yc-x,5,5);
  }

  public static void main(String args[])
  {
   Scanner sc=new Scanner(System.in);
   System.out.println("Enter center (xc,yc): ");
   int xc=sc.nextInt();
   int yc=sc.nextInt();
   System.out.println("Enter radius r: ");
   int r=sc.nextInt();
   MP_CIRCLE c = new MP_CIRCLE(xc,yc,r);
   JFrame f=new JFrame();
   f.add(c);
   f.setSize(800,800);
   f.setVisible(true);
  }
}