var app = angular.module('controllers.rgbtoui', ['ngRoute', 'ui.bootstrap', 'colorpicker.module']);

app.controller("RgbToUICtrl", function(appConfig, $scope, $filter, $rootScope) {

    $scope.title = "RGB to UIColor Converter";

    // Roll Tide
    $scope.rgbValid = true;
    $scope.alphaValid = true;
    $scope.alpha = "1.0";

    $scope.color = {
        "rgb": "",
        "r": "",
        "g": "",
        "b": "",
    };

    function convertRgbToUi(color) {

        $scope.color.rgb = color;
        $scope.color.r = color.replace(/^rgba?\(|\s+|\)$/g,'').split(',')[0];
        $scope.color.g = color.replace(/^rgba?\(|\s+|\)$/g,'').split(',')[1];
        $scope.color.b = color.replace(/^rgba?\(|\s+|\)$/g,'').split(',')[2];

        $scope.uiColor = {
            "r": ($scope.color.r / 255).toFixed(2),
            "g": ($scope.color.g / 255).toFixed(2),
            "b": ($scope.color.b / 255).toFixed(2),
        };

        console.log($scope.color);

        $rootScope.$broadcast('ColorChanged', $scope.color);

    }





    $scope.$watch('color', function(newVal, oldval){

        if (newVal.r && newVal.g && newVal.b) {

            if (!isNaN(newVal.r) && !isNaN(newVal.g) && !isNaN(newVal.b)) {
                $scope.color.rgb = "rgb(" + $scope.color.r + "," + $scope.color.g + "," + $scope.color.b + ")";
                $rootScope.$broadcast('ColorChanged', $scope.color.rgb);

                $scope.rgbValid = true;
                $scope.uiColor = {
                    "r": newVal.r,
                    "g": newVal.g,
                    "b": newVal.b,
                };
                console.log("------");
            } else {
                $rootScope.$broadcast('ColorChanged', appConfig.themePrimary);
                $scope.rgbValid = false;
                console.log("Invalid.");
            }

        } else {
            $rootScope.$broadcast('ColorChanged', appConfig.themePrimary);
        }

    }, true);










    $scope.$watch('rgb', function(newVal, oldval){
        if (newVal) {
            convertRgbToUi(newVal);
            updateCopyText();
        } else {
            $rootScope.$broadcast('ColorChanged', appConfig.themePrimary);
        }
    }, true);

    function updateCopyText() {
        $scope.copyObjectiveC = "[UIColor colorWithRed:" + $scope.uiColor.r + " green:" + $scope.uiColor.g + " blue:" + $scope.uiColor.b + " alpha:" + $scope.alpha + "];";
        $scope.copySwift = "UIColor(red:" + $scope.uiColor.r + ", green:" + $scope.uiColor.g + ", blue:" + $scope.uiColor.b + ", alpha:" + $scope.alpha + ")";
    }


});
@@ -458,25 +458,24 @@
 Ui(color) %7B%0A
-%0A
         $sco
@@ -957,25 +957,50 @@
 ope.color);%0A
+        updateCopyText();
 %0A
-
         $roo
@@ -1052,20 +1052,15 @@
 r);%0A
-%0A
     %7D%0A%0A
-%0A%0A%0A%0A
     
@@ -1108,17 +1108,16 @@
 ldval)%7B%0A
-%0A
         
@@ -1365,301 +1365,39 @@
     
-$rootScope.$broadcast('ColorChanged', $scope.color.rgb);%0A%0A                $scope.rgbValid = true;%0A                
+convertRgbToUi(
 $scope.
-uiC
+c
 olor
- = %7B%0A                    %22r%22: newVal.r,%0A                    %22g%22: newVal.g,%0A                    %22b%22: newVal.b,%0A                %7D;%0A                console.log(%22------%22
+.rgb
 );%0A 
@@ -1572,16 +1572,20 @@
 %22Invalid
+ RGB
 .%22);%0A   
@@ -1694,25 +1694,24 @@
 ;%0A        %7D%0A
-%0A
     %7D, true)
@@ -1713,25 +1713,16 @@
 true);%0A%0A
-%0A%0A%0A%0A%0A%0A%0A%0A%0A
     $sco
@@ -1826,38 +1826,8 @@
 l);%0A
-            updateCopyText();%0A
     
@@ -2306,15 +2306,14 @@
 )%22;%0A
+
     %7D%0A%0A
-%0A
 %7D);

Clean up & better functionality