The bitwise operator is not functioning as anticipated

My goal is to develop a simple tool for myself that can convert values between rgb and hex formats. It's mostly working fine, but there is one issue that I have encountered.

When I input a hex value like '007aff', the leading '00' gets removed and the resulting value is '7aff'. While the r/g/b values are correct, I do not want the zeroes to be trimmed from the hexadecimal value. Is there something wrong with my approach?

// For those unfamiliar with angular
// $watch triggers when the variable in quotes changes its value
// The rest of the code is basic javascript

AngularJS:

  $scope.$watch('hex', function() {

    var rgb = parseInt($scope.hex, 16);

    $scope.r = (rgb >> 16) & OxFF;
    $scope.g = (rgb >> 8) & OxFF;
    $scope.b = rgb & 0xFF;

    $scope.rgb = 'rgb(' + $scope.r + ',' + $scope.g + ',' + $scope.b + ');';

  });

  $scope.$watch('r+g+b', function() {

    $scope.rgb = 'rgb(' + $scope.r + ',' + $scope.g + ',' + $scope.b + ');';
    $scope.hex = parseInt($scope.r << 16 | $scope.g << 8 | $scope.b).toString(16);
  });

Check out this sample Plunker to see it in action:

Answer №1

The 00 are not directly cut. When you change the rgb value to a text, you do not include leading zeros.

Give this method a try for including leading zeros:

let number = parseInt($scope.r << 16 | $scope.g << 8 | $scope.b);
$scope.hex = ('000000' + number.toString(16)).slice(-6);

Answer №2

Below:

$scope.hex = parseInt($scope.r << 16 | $scope.g << 8 | $scope.b).toString(16);

insert the code block below:

var hexLength = $scope.hex.length;
for ( var j = 0 ; j < 6 - hexLength ; j++ ) {
  $scope.hex = '0' + $scope.hex;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Looking to replicate a Modal that I designed, but unsure which elements need altering in order to achieve this. I am hoping to create three duplicates of the Modal

This modal is functioning perfectly, and now I want to replicate the same modal three times on a single page. I require three distinct buttons on the same page to trigger these separate modals. At this point, I am unsure which attributes need modification ...

The initialization of the Angular service is experiencing issues

I have a service in place that checks user authentication to determine whether to redirect them to the login page or the logged-in area. services.js: var servicesModule = angular.module('servicesModule', []); servicesModule.service('login ...

How can an AngularJS/Ionic mobile application incorporate an external JavaScript script?

Can external JavaScript scripts be executed in an AngularJS/Ionic mobile app, particularly on non-jailbroken iOS devices? We are considering creating a launcher version of our app that downloads the required scripts and then installs and runs them. I have ...

TypeError: The 'username' property is unreadable because it is undefined

I am currently learning Ionic and MySQL and I am trying to create a login form with a remote database. However, I am encountering an issue where I receive the error message "TypeError: Cannot read property 'username' of undefined." Here is the c ...

Displaying an error message prior to submitting the form in an AngularJS form validation process

I have implemented form validation using angularjs, but I am encountering an issue where the error message is displayed upon page load instead of after an actual error. I have set up a validation summary using a directive. Can you please advise me on how t ...

What is the proper way to include 'rowspan' specific CSS within an HTML table?

I have an HTML table with rowspans in it: table tr, td { border: 1px solid black; } tr:nth-child(1) { background-color: red; } tr:nth-child(2) { background-color: blue; } <table> <tr> <td rowspan=2>Section 1</td> ...

How to send arguments to an external JavaScript file with JavaScript

In the header of my HTML document, I have included the following script: <script src="http://www.domain.com/js/widgets.js" type="text/javascript"></script> This script references: widgets.js (function () { var styleEl = document.create ...

Is there a way to use JavaScript/jQuery to randomly resize images every time the page loads

Instead of generating thumbnails for my images, I am interested in displaying the original images on my page. However, I still need to resize these images for optimal viewing. Instead of applying a fixed width of 200px, I would like each image to be displa ...

Encountering difficulties in loading my personal components within angular2 framework

I encountered an issue while trying to load the component located at 'app/shared/lookup/lookup.component.ts' from 'app/associate/abc.component.ts' Folder Structure https://i.stack.imgur.com/sZwvK.jpg Error Message https://i.stack.img ...

Unable to convert the existing JSON array into the specified type

I've been struggling to find a solution to this problem, even though I've come across similar topics that have been asked before. Below is a sample Json that I'm posting to a web API controller: { "AppointmentId ":2079, "ma ...

When a React Router link is activated, the React Bootstrap dropdown remains open instead of closing as expected

<NavDropdown className="userDropdownButton" title="dropdown" id="user-nav-dropdown" alignRight > <div className="userDropDown"> <Link to="/user" className="userDropDownheader"> user </Link> </div> < ...

Ways to transfer information from the server to the user interface in a WordPress extension

I am currently developing a WordPress plugin and have successfully created a form in the admin panel. Now, I am looking to transfer the data collected from that form to my Frontend JavaScript file. Is there a way to achieve this, and if so, what steps shou ...

Turn off Angular form validation for individual input field (URL)

I am in the process of creating an Angular form that includes a URL field which users can input with or without the http:// prefix. The default URL validation in Angular requires the prefix, so I have incorporated a regex pattern that allows for both optio ...

Getting a specific piece of information from a JSON file

I am encountering an issue with my JSON file collection. When I access it through http://localhost:5000/product/, I can see the contents without any problem. However, when I try to retrieve a specific product using a link like http://localhost:5000/product ...

Sharing and showcasing files directly from a local directory

Recently diving into NodeJS and web development, I've successfully used multer to upload a single file within my web application. The file gets uploaded to my "uploads" folder flawlessly, and now I'm planning on storing the file path in my databa ...

Ensuring the validity of input tags

I encountered an issue with an input tag that has a specific logic: https://codepen.io/ion-ciorba/pen/MWVWpmR In this case, I have a minimum value retrieved from the database (400), and while the logic is sound, the user experience with the component lea ...

Customize the styling of an individual 'LI' item within an array by utilizing Jquery

Just starting out with Javascript and jQuery. Created a basic image slider that's running into a jQuery problem. Here's the HTML for the image list: <div id = "slider-auto"> <ul class= "slides"> <li class = "slide"&g ...

What are the steps for combining AngularJS with Java JAAS authentication system?

My web application combines AngularJS on the frontend with Java on the backend. Angular communicates with the Java backend through Restful webservices exchanging JSON data over HTTP. I am in need of developing an authentication mechanism for this app and I ...

Are there any more efficient methods to retrieve an object from an arrow function in TypeScript?

Trying to retrieve an object from an arrow function is posing a challenge for me, especially with the following function f: myMethod(f: data => { return { someField: data.something }; }); I am aware that for simple types, you can condense the arrow ...

What steps should I take to incorporate this feature into my Meteor project?

After successfully installing the type.js package, I discovered that the code works fine when directly executed in the console. Here is the code snippet: $(function(){ $(".typedelement").typed({ strings: ["You don&apo ...